[Tutor] getting the current method name
antonmuhin at rambler.ru
antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Thu Apr 17 12:20:53 2003
Hello Adam,
Thursday, April 17, 2003, 11:11:02 AM, you wrote:
AG> Dear All,
AG> how to do the following:
AG> class x:
AG> def y():
AG> print "Hi, my name is",<<method-name --> 'y'>>
AG> z=x()
AG> z.y()
AG> should print
AG> "Hi, my name is y"
AG> Of course, resolving "<<method-name --> 'y'>>" should not be done with
AG> simply 'y' :-)
AG> Adam
Try the following:
import sys
def get_callee_name(depth = 0):
return sys._getframe(depth + 1).f_code.co_name
def foo():
print get_callee_name()
class Bar:
def bar(self):
print get_callee_name()
def bar_with_class(self):
print "%s.%s" % (self.__class__.__name__, get_callee_name())
print "Calling foo"
foo()
print "Calling Bar.bar()"
Bar().bar()
print "Calling Bar.bar_with_class()"
Bar().bar_with_class()
--
Best regards,
anton mailto:antonmuhin@rambler.ru