Getting method name from within the class method

Gabriel Genellina gagsl-py at
Thu Oct 19 01:09:40 CEST 2006

At Wednesday 18/10/2006 19:30, yellowalienbaby at 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!

More information about the Python-list mailing list