Getting method name from within the class method

M

Mitko Haralanov

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!
 
Y

yellowalienbaby

Mitko said:
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;

.... def a_method(self,this,that):
.... print self.a_method.__name__
....a_method
 
L

Larry Bates

Mitko said:
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
 
M

Mitko Haralanov

On 18 Oct 2006 14:38:12 -0700
... 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.
 
Y

yellowalienbaby

Mitko said:
On 18 Oct 2006 14:38:12 -0700


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 :)
 
G

Gabriel Genellina

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
 
Y

yellowalienbaby

Gabriel said:
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.
 
M

Marc 'BlackJack' Rintsch

Mitko Haralanov said:
On 18 Oct 2006 14:38:12 -0700


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
 
G

Gabriel Genellina

At said:
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
 
S

Steven D'Aprano

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:
My name is method_0
My result is 2My name is method_7
My result is 10.4
 
G

George Sakkis

Mitko said:
On 18 Oct 2006 14:38:12 -0700


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
 
F

Fredrik Lundh

Mitko said:
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>
 
F

Fredrik Lundh

Gabriel said:
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>
 
M

Mitko Haralanov

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

Mitko Haralanov

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!!
 
B

Ben Finney

Mitko Haralanov said:
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.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top