By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

how can I get the name of a method inside it?

P: n/a
I would like to know if it's possible to retrieve the name of a method when
you're inside it. For example, in the following script, I would like to
assign _s so that it prints "you are in method1".
***************************************
class Obj1:
def __init__(self):
...

def method1(self):
_s = ???
print "you are in %s" % _s

x = Obj1()
x.method1()
Jun 3 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I would like to know if it's possible to retrieve the name of a method when
you're inside it. For example, in the following script, I would like to
assign _s so that it prints "you are in method1".
***************************************
class Obj1:
def __init__(self):
...

def method1(self):
_s = ???
print "you are in %s" % _s

x = Obj1()
x.method1()
i'm no expert on the subject, but AFAIK, there's no way to do this.
why not just print "you are in method1"?
what are you exactly trying to do?

Jun 3 '07 #2

P: n/a
"Sébastien Vincent" <sebastien_nimp73<@wrote:
I would like to know if it's possible to retrieve the name of a method when
you're inside it. For example, in the following script, I would like to
assign _s so that it prints "you are in method1".
***************************************
class Obj1:
def __init__(self):
...

def method1(self):
_s = ???
print "you are in %s" % _s
For debugging purposes,

_s = sys._getframe(0).f_code.co_name

should work (after you've done an "import sys" somewhere appropriate, of
course;-). The _ in front of _getframe, as well as the klunkiness of it
all, are all indications that this is _not_ recommended for "production
use" -- like most of Python's introspection features, it IS chiefly
meant for debugging purposes.
Alex
Jun 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.