access the name of my method inside it

Steve Holden steve at holdenweb.com
Wed Aug 1 08:30:15 EDT 2007


Marc 'BlackJack' Rintsch wrote:
> On Wed, 01 Aug 2007 07:01:42 -0400, Steve Holden wrote:
> 
>> Marc 'BlackJack' Rintsch wrote:
>>> On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>>
>>>> for example I have this method
>>>>
>>>> def my_method():
>>>>     # do something
>>>>
>>>>     # how do I get the name of this method which is my_method here?
>>> Why do you need this?  There are ways but those are not really good for
>>> production code.
>>>
>> Maybe he wants to write a recursive method?
>>
>> Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?
> 
> Ugly yes, unnecessary convoluted yes, solution no.  You typed `my_method`
> in the source.  The OP wants to know how to avoid that.
> 
>>  >>> class p:
>> ...   def mymethod(self, n):
>> ...     if n <= 1:
>> ...       return 1
>> ...     else:
>> ...       return n * self.__class__.mymethod(self, n-1)
> 
> Why not simply ``self.mymethod(n - 1)`` instead!?
> 
Well, absolutely no reason if you want to go making things simple ;-)

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list