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

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)

Leave a Reply