本文介绍了访问其中的方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 例如我有这个方法 def my_method(): #do一些东西 #我怎么得到这个my_method这个方法的名字? 谢谢, james 解决方案 为什么需要这个?有一些方法,但那些对于 生产代码并不是很好。 Ciao, Marc''BlackJack''Rintsch 你为什么需要这个?有一些方法,但那些对于 生产代码并不是很好。 也许他想写一个递归方法? 一旦打电话给自己.__ calss __。mymethod(self)。丑陋,不是吗? .... def mymethod(self,n): ....如果n< = 1: ....返回1 ....其他: ....返回n * self .__ class __。mymethod (self,n-1) .... 3628800 问候 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 - ---------------- 上网:博客,镜头和互联网标签 许多服务目前提供免费注册 -----------感谢您阅读------------- 为什么需要这个?有一些方法,但那些对于生产代码并不是很好。 也许他想写一个递归方法? 一旦打电话给自己.__ calss __。mymethod(self)。丑陋,不是吗? 丑陋的是,不必要的复杂的是,解决方案没有。你在源代码中键入了'my_method` 。 OP希望知道如何避免这种情况。 ... 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 methoddef my_method():# do something# how do I get the name of this method which is my_method here?Thanks,james 解决方案Why do you need this? There are ways but those are not really good forproduction code.Ciao,Marc ''BlackJack'' RintschWhy do you need this? There are ways but those are not really good forproduction code.Maybe he wants to write a recursive method?Once way is to call self.__calss__.mymethod(self). Ugly, isn''t it?.... def mymethod(self, n):.... if n <= 1:.... return 1.... else:.... return n * self.__class__.mymethod(self, n-1)....3628800regardsSteve--Steve Holden +1 571 484 6266 +1 800 494 3119Holden Web LLC/Ltd http://www.holdenweb.comSkype: holdenweb http://del.icio.us/steve.holden--------------- Asciimercial ------------------Get on the web: Blog, lens and tag the InternetMany services currently offer free registration----------- Thank You for Reading ------------- Why do you need this? There are ways but those are not really good forproduction 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.... 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 这篇关于访问其中的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!