00__00__00
00__00__00

Reputation: 5347

programmatically setting names of lambda function

I have a lambda function

myfun=lambda x:2*x

if I print its name

print(myfun)
<function <lambda> at 0x000000000E512898>

Which is not very informative.

If I set its name explicitly, it works better

myfun.__name__='myfun'
print(myfun)
<function myfun at 0x000000000E512898>

Given that I have a large set of lambdas, how to perform this assignment programmatically?

The motivation for doing is is the following:

I am importing another function

from mymodule import f_many_args

In mymodule, it is defined as

def f_many_args(a,b,c):

However, in another script, I need to call this f_many_args by setting b and c using some global variables. Later I need to print the name of f_many_args and I needed it to be somewhere more informative than

Upvotes: 1

Views: 280

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36702

replace:

myfun = lambda x:2*x

with:

def myfun(x):
    """doubles the input parameter"""
    return 2 * x

Upvotes: 3

Related Questions