access the name of my method inside it
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Wed Aug 1 07:10:21 EDT 2007
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!?
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list