Finding a function name

T

Tim Williams

[ 1 step up from Newbie ]

How can I find a function's name from within the function?
trailing_period
trailing_period

but from within the function itself, print (either option) gives
"container"

TIA
 
C

Christopher T King

How can I find a function's name from within the function?

You can't (yet), in a top-level function at least. In an object's
method, you can do this:

class a(object):
def foo(self):
print self.foo.__name__

But that requires you to know the function's name to begin with ;)

Going off on a tangent, this is a case where some sort of reverse function
attributes would be of help:

def foo(a,b,c):
.someattr = "something" # <-- proposed function attribute,
# assigned at compile time

print .someattr, .__name__ # <-- proposed reverse function attribute,
# accessed at runtime

But right now there's no such thing in Python.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top