访问其中的方法名称 [英] access the name of my method inside it
问题描述
例如我有这个方法
def my_method():
#do一些东西
#我怎么得到这个my_method这个方法的名字?
谢谢,
james
2007年8月1日星期三09:06:42 +0000,james_027写道:
例如我有这个方法
def my_method():
#做点什么
>
#如何获取此方法的名称my_method?
为什么需要这个?有一些方法,但那些对于
生产代码并不是很好。
Ciao,
Marc''BlackJack''Rintsch
Marc''BlackJack''Rintsch写道:
2007年8月1日星期三09:06: 42 +0000,james_027写道:
>例如我有这个方法
def my_method():
#do
#我如何在这里获得my_method这个方法的名称?
你为什么需要这个?有一些方法,但那些对于
生产代码并不是很好。
也许他想写一个递归方法?
一旦打电话给自己.__ calss __。mymethod(self)。丑陋,不是吗?
>> class p:
.... def mymethod(self,n):
....如果n< = 1:
....返回1
....其他:
....返回n * self .__ class __。mymethod (self,n-1)
....
>> pp = p()
pp.mymethod(10)
3628800
< blockquote class =post_quotes>
>>>
问候
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 - ----------------
上网:博客,镜头和互联网标签
许多服务目前提供免费注册
-----------感谢您阅读-------------
2007年8月1日星期三07:01:42 -0400,Steve Holden写道:
Marc''BlackJack''Rintsch写道:
> 2007年8月1日星期三09:06:42 + 0000,james_027写道:
>>例如我有这个方法
def my_method():
#做点什么
#我怎么得到这个方法的名字是my_method吗?
为什么需要这个?有一些方法,但那些对于
生产代码并不是很好。
也许他想写一个递归方法?
一旦打电话给自己.__ calss __。mymethod(self)。丑陋,不是吗?
丑陋的是,不必要的复杂的是,解决方案没有。你在源代码中键入了'my_method`
。 OP希望知道如何避免这种情况。
>> class p:
... def mymethod(self,n):
...如果n< = 1:
...返回1
...其他:
...返回n * self .__ class __。mymethod(self,n-1)
为什么不简单地``self.mymethod(n-1)``而不是!?
Ciao,
Marc''BlackJack''Rintsch
Hi,
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?
Thanks,
james
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.
Ciao,
Marc ''BlackJack'' Rintsch
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?
>>class p:
.... def mymethod(self, n):
.... if n <= 1:
.... return 1
.... else:
.... return n * self.__class__.mymethod(self, n-1)
....>>pp = p()
pp.mymethod(10)
3628800
>>>
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 -------------
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
这篇关于访问其中的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!