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

Getting method name from within the class method

P: n/a
I need to be able to get the name of the currently executed method
within that method. I know that the method object does have the
__name__ attribute but I know know how to access it from withing the
method.

Something like this:

class A:
.....
def a_method (self, this, that):
print <__name__>

a = A ()
a.a_method()
'a_method'
Thanx for your help!
--
Mitko Haralanov mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

Oct 18 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a

Mitko Haralanov wrote:
I need to be able to get the name of the currently executed method
within that method. I know that the method object does have the
__name__ attribute but I know know how to access it from withing the
method.

Something like this:

class A:
....
def a_method (self, this, that):
print <__name__>

a = A ()
a.a_method()
'a_method'
Thanx for your help!
I'm not sure this is what you want, it seems redundant to me so i am
probably misunderstanding something, but I did;

>>class test(object):
.... def a_method(self,this,that):
.... print self.a_method.__name__
....
>>a=test()
a.a_method('this','that')
a_method

Oct 18 '06 #2

P: n/a
Mitko Haralanov wrote:
I need to be able to get the name of the currently executed method
within that method. I know that the method object does have the
__name__ attribute but I know know how to access it from withing the
method.

Something like this:

class A:
....
def a_method (self, this, that):
print <__name__>

a = A ()
a.a_method()
'a_method'
Thanx for your help!
Since you KNOW you are in the method, you can hardcode it:

def a_method(self, this, than):
print "a_method"

There's nothing to be gained from accessing a variable unless
you are going to be calling some other method to do the printing.

-Larry
Oct 18 '06 #3

P: n/a
On 18 Oct 2006 14:38:12 -0700
ye*************@gmail.com wrote:
>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__
Doing the above will obviously work!

However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.

--
Mitko Haralanov mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

Oct 18 '06 #4

P: n/a

Mitko Haralanov wrote:
On 18 Oct 2006 14:38:12 -0700
ye*************@gmail.com wrote:
>>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__

Doing the above will obviously work!

However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.
Thats what I thought..
I dont know if there is a python way of doing it, but if replacing text
is the only concern, wouldnt a good editor with regex search+replace
make the job easier ?

or if your in a unix with ksh / pdksh

for f in $(ls)
do
sed -e "s/print self.a_method.__name__/print self.new_name.__name/g"
done

or similar...

I'm not helping much, am I :)

Oct 18 '06 #5

P: n/a
At Wednesday 18/10/2006 18:59, Mitko Haralanov wrote:
>>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__

Doing the above will obviously work!

However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.
Use the inspect module.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 18 '06 #6

P: n/a
for f in $(ls)
do
sed -e "s/print self.a_method.__name__/print self.new_name.__name/g"
done
thats a terrible bit of broken shell code, sorry !

Oct 18 '06 #7

P: n/a

Gabriel Genellina wrote:
At Wednesday 18/10/2006 18:59, Mitko Haralanov wrote:
>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__
Doing the above will obviously work!

However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.

Use the inspect module.
--
Gabriel Genellina
Softlab SRL

I'm sure the OP has good reasons, and I dont want to be arguing with
anyone, but I am curious. If I found myself in a similar situation I
wouldn't try to include code in whatever I'm writing that simply aids
my writing of the code; it's irrelevant to the application and in my
mind, shouldn't be there. its probably only a tiny bit of extra
resource etc.. included into the app's use of resources and hence not
really so important, but I would feel happier with my end code if I
hadn't done such a thing.

Oct 18 '06 #8

P: n/a
In <ma**************************************@python.o rg>, Mitko Haralanov
wrote:
On 18 Oct 2006 14:38:12 -0700
ye*************@gmail.com wrote:
>>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__

Doing the above will obviously work!

However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.
class test(object):
def a_method(self, this, that):
NAME = 'a_method'
print NAME
# ...
print NAME
# ...

And please don't come back and complain that you have to type and probably
change 'a_method' twice instead of once. ;-)

Ciao,
Marc 'BlackJack' Rintsch
Oct 18 '06 #9

P: n/a
At Wednesday 18/10/2006 19:30, ye*************@gmail.com wrote:
>>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__
>
>Doing the above will obviously work!
>
>However, I don't want to have to use the name of the function in the
>print statement (the ".a_method." part). Imagine having about 100 of
>the above print statements in the function and then you change the name
>of the function. I want all 100 of the print statements to work without
>having to change every one of them to reflect the new function name.
Use the inspect module.
I'm sure the OP has good reasons, and I dont want to be arguing with
anyone, but I am curious. If I found myself in a similar situation I
wouldn't try to include code in whatever I'm writing that simply aids
my writing of the code; it's irrelevant to the application and in my
mind, shouldn't be there. its probably only a tiny bit of extra
resource etc.. included into the app's use of resources and hence not
really so important, but I would feel happier with my end code if I
hadn't done such a thing.
I could see some merit on getting that info in an automatic way.
The only reason I can see for knowing the name of a function is for
debugging purposes - maybe some kind of logging utility. If you are
in "debug mode", resources are not too important, but correct
information is. Imagine a logfile that says that you were at
function_a but instead you were at function_b (because of copy&paste
without replacing the names)
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 18 '06 #10

P: n/a
On Wed, 18 Oct 2006 13:59:55 -0700, Mitko Haralanov wrote:
I need to be able to get the name of the currently executed method
within that method. I know that the method object does have the
__name__ attribute but I know know how to access it from withing the
method.
Here is a useful (moderately advanced) technique:

def factory(arg):
def foo(x=arg):
print "My name is", foo.__name__
print "My result is", x + 1
return foo

class spam:
pass

for i in range(10):
name = "method_%d" % i
f = factory(i)
f.__name__ = name
setattr(spam, name, staticmethod(f))
But:
>>spam.method_0(1)
My name is method_0
My result is 2
>>spam.method_7(9.4)
My name is method_7
My result is 10.4
--
Steven.

Oct 18 '06 #11

P: n/a
Mitko Haralanov wrote:
On 18 Oct 2006 14:38:12 -0700
ye*************@gmail.com wrote:
>>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__

Doing the above will obviously work!

However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.
from inspect import getframeinfo,currentframe

class test(object):
def a_method(self,this,that):
print getframeinfo(currentframe())[2]
If you *really* have about 100 functions or methods you want to print
their names, don't copy and paste this all over the place; use a
decorator instead so that you can just write:

class test(object):
@print_name
def a_method(self,this,that):
pass

@print_name
def b_method(self,this,that):
pass

The definition of print_name is left as an exercise to the reader.

HTH,
George

Oct 19 '06 #12

P: n/a
Mitko Haralanov wrote:
>>>>class test(object):
... def a_method(self,this,that):
... print self.a_method.__name__

Doing the above will obviously work!
so will

print "a_method"

of course. no need to be silly when you don't have to.
However, I don't want to have to use the name of the function in the
print statement (the ".a_method." part). Imagine having about 100 of
the above print statements in the function and then you change the name
of the function. I want all 100 of the print statements to work without
having to change every one of them to reflect the new function name.
why are you writing functions that needs to output their own name a
100 times? why should the program's *external* behaviour depend on
such an irrelevant detail of its internal design? sounds like lousy
design to me.

if you want to tag 100 messages with the same name, and you want to make
it easy to change that name, should the need arise, use a variable:

def a_method(self, this, that):
name = "myname"

print name, "is running"
print name, "is about to return"

</F>

Oct 19 '06 #13

P: n/a
Gabriel Genellina wrote:
I could see some merit on getting that info in an automatic way.
The only reason I can see for knowing the name of a function is for
debugging purposes - maybe some kind of logging utility. If you are in
"debug mode", resources are not too important, but correct information
is. Imagine a logfile that says that you were at function_a but instead
you were at function_b (because of copy&paste without replacing the names)
imagine a log file that says that you're about to create a file when
you're actually removing it (because of copy&paste without replacing the
message).

focussing on function names when logging is pretty silly, anyway. it's
usually better to focus on that the code is actually doing, rather than
internal artifacts.

but even if you want to output function names, it's of course better to
put that functionality into the logging function itself:

##
# Issues a log message.

def log(fmt, *args):
print who_called_me(2), "-", fmt % args

##
# Returns the name of the calling function or method, if known.
#
# @param depth Stack depth. Defaults to immediate caller.
# @return The name of the calling function.

def who_called_me(depth=1):
return sys._getframe(depth).f_code.co_name

</F>

Oct 19 '06 #14

P: n/a
on that

on what

</F>

Oct 19 '06 #15

P: n/a
On Thu, 19 Oct 2006 08:16:57 +0200
Fredrik Lundh <fr*****@pythonware.comwrote:
why are you writing functions that needs to output their own name a
100 times? why should the program's *external* behaviour depend on
such an irrelevant detail of its internal design? sounds like lousy
design to me.
I am sorry if I sound defensive in my reply here, I am not trying to be.

What does it matter why I am doing it? I only want to know if it's
possible and if so, how?

I only gave the above scenario as an example to illustrate my point
and so I don't have to explain the design and internals of my work.

--
Mitko Haralanov mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

Oct 20 '06 #16

P: n/a
On 18 Oct 2006 21:01:36 -0700
"George Sakkis" <ge***********@gmail.comwrote:
from inspect import getframeinfo,currentframe

class test(object):
def a_method(self,this,that):
print getframeinfo(currentframe())[2]
Thanx for the reply! This about the most useful one I've gotten so far!!

--
Mitko Haralanov mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

Oct 20 '06 #17

P: n/a
Mitko Haralanov <mi***@qlogic.comwrites:
On Thu, 19 Oct 2006 08:16:57 +0200
Fredrik Lundh <fr*****@pythonware.comwrote:
why are you writing functions that needs to output their own name
a 100 times? why should the program's *external* behaviour depend
on such an irrelevant detail of its internal design? sounds like
lousy design to me.

I am sorry if I sound defensive in my reply here, I am not trying to
be.
I believe Frederik's abrasive approach is an attempt to help you be
dispassionate about your design, so that possible flaws can be
examined rationally.
What does it matter why I am doing it? I only want to know if it's
possible and if so, how?
Most programming forums, this one included, see a steady flow of
people who, having gone some way down a particular path, believe that
all they need is to be shown how to complete the journey. An
examination of the *actual* problem to be solved often shows that
there is a better way to go.

We prefer to know what actual problem is to be solved, so that false
starts can be terminated quickly, and a practical approach suggested
instead. Without knowing what the actual problem is, we can't know
what to suggest.

--
\ "There was a point to this story, but it has temporarily |
`\ escaped the chronicler's mind." -- Douglas Adams |
_o__) |
Ben Finney

Oct 20 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.