473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Getting method name from within the class method

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
17 21867

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

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

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
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
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
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
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
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
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
on that

on what

</F>

Oct 19 '06 #15
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Leonard Rutkowski | last post by:
I am using a third party product that requires a reference to a page, Me.Page for example. I would like to use this product in a function, in a vb class, that I am writing. I know that I can pass...
0
by: Yuvraj Sujan | last post by:
Is there a generic way of getting the method metadata from within the method itself e.g. consider a method void knowThyself(string someParam_in ) { //want to know the information about...
4
by: Nikolay Petrov | last post by:
Hi guys, I am implementing a loging system in my app and I need a way to get name of current class and name of the current method, so I can pass them to my loging system. Also I want to asign...
12
by: peregrine_falcon12 | last post by:
Is there a way to get a pointer to a class from inside one of the class's methods?
2
by: Mukesh | last post by:
Hi, Could I write a code that will get the method name within which the code is written. like I want void showMethodName(){ string method; method=Need a method here to get the method name...
9
by: kj7ny | last post by:
Is there a way that I can programmatically find the name of a method I have created from within that method? I would like to be able to log a message from within that method (def) and I would like...
0
by: Donn Ingle | last post by:
In an unusual twist of code I have a subclass which overrides a method but it also needs to call the original method: class One: def add (self, stuff): self.stuff.append(stuff) class...
8
Jezternz
by: Jezternz | last post by:
Ok, I am new to JS OOP, so bare with me. The code is quite large so I will summaries and simplify, to show the piece that isn't working. Ok I have defined a class and then an object with several...
5
by: furqi | last post by:
hi every body i write a code in c sharp in which i have made a base class and make an event there.Then i make a derived class and made an other event in that class. Now what i wanna do is that i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.