1

I have a very long list of functions and I am hoping to be able to call them and have them print their name and when they are completed as show below.

def call(*functions):
    for f in functions:
        print(f.__name__)
        f()
        print('{} completed'.format(f.__name__))

call(lambda: (long(), lst(), of(), func(), ions()...))

I do not want to have to write print(f.__name__) ... print('{} completed'.format(f.__name__)) around every function. However, in the code above it prints 'lambda' (as expected). how can I automate these function calls/print statements so it prints correctly?

0

2 Answers 2

1
def call(*functions):
    for f in functions:
        print(f.__name__)
        f()
        print('{} completed'.format(f.__name__))

call(long, lst, of, func, ions...)

However, it would be more logical to use map and simplify call:

def call(function):
    print(f.__name__)
    f()
    print('{} completed'.format(f.__name__))

map(call, (long, lst, of, func, ions...))
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to do this where each function has different arguments? I'm playing with functools.partial, but 'functools.partial' object has no attribute __name__
Yes but it becomes metaprogramming using a decorator like pattern and so on.... (Sorry I'm on my phone and can't help on it more right now but ill have a look at it...)
@Apero no worries, I figured it out. Instead of f.__name__ which creates a partial object, you need to call f.func.__name__ which prints the function's name
1

You nearly have the right idea, but you want to pass the functions themselves to call rather than a lambda.

As your code currently stands, you are only passing one function, the lambda, which calls the other functions between your print statements.

Change

call(lambda: (long(), lst(), of(), func(), ions()...))

to

call(long, lst, of, func, ions...))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.