Finding a function name

Christopher T King squirrel at WPI.EDU
Fri Aug 6 18:41:36 CEST 2004


On Fri, 6 Aug 2004, Tim Williams wrote:

> 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.




More information about the Python-list mailing list