Function reference -> name?

R

Rene Pijlman

When I have a reference to a function (as in parameter 'function' of f1
below), how can I get the name of that function ('f2' in the example
below)?

def f1(function):
pass
# How to get 'f2' from function?

def f2():
pass

f1(f2)
 
E

Emile van Sebille

Rene Pijlman said:
When I have a reference to a function (as in parameter 'function' of f1
below), how can I get the name of that function ('f2' in the example
below)?

def f1(function):
pass
# How to get 'f2' from function?

def f2():
pass

f1(f2)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__ge
tattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__r
educe__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'func_closure',
'func_code', 'func_defaults', 'func_dict', 'func_doc',
'func_globals', 'func_na
me']




HTH,
 
D

Duncan Booth

When I have a reference to a function (as in parameter 'function' of f1
below), how can I get the name of that function ('f2' in the example
below)?

def f1(function):
pass
# How to get 'f2' from function? print function.__name__

def f2():
pass

f1(f2)
This will give you the name under which the function was originally
defined, which of course may not be the name of the variable at the time
you passed it in. Also if it was a lambda function you get the name
'<lambda>', and if it is some other sort of callable object it may or may
not have a __name__ attribute.
 
R

Rene Pijlman

Emile van Sebille:
Rene Pijlman:
When I have a reference to a function, how can I get the name of that function? [...]

Of course.

I forgot to mention that the function reference is an XML-RPC client stub,
which makes quite a difference :)

dir() told me I need functionReference._Method__name in that case.

Thanks Emile and Duncan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,921
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top