Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this function passed as an argument to another function return None?

def run_my_func(runner, *args, **kwargs):
    runner(*args)
    
def my_func(name):
    return name
    
out = run_my_func(my_func, "my name is john", kwargs=None)

print(out)

Why does this print out None?

If I print out the name like so:

def run_my_func(runner, *args, **kwargs):
    runner(*args)
    
def my_func(name):
    print(name)
    return name
    
out = run_my_func(my_func, "my name is john", kwargs=None)

print(out)

I get the output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my name is john
None

>Solution :

Your function doesn’t return the value from the function it calls:

def run_my_func(runner, *args, **kwargs):
    return runner(*args)
    
def my_func(name):
    return name
    
out = run_my_func(my_func, "my name is john", kwargs=None)

print(out)

Output as requested.

Also, you can safely use both args and kwargs in calling runner, even if it takes no arguments:

def run_my_func(runner, *args, **kwargs):
    return runner(*args, **kwargs)
    
def my_func(name):
    return name
    
out = run_my_func(my_func, "my name is john")

print(out)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading