python:如何从其中引用类(如递归函数) [英] python: how to refer to the class from within it ( like the recursive function)
本文介绍了python:如何从其中引用类(如递归函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于递归函数,我们可以执行以下操作:
For a recursive function we can do:
def f(i):
if i<0: return
print i
f(i-1)
f(10)
但是有什么方法可以做以下事情?
However is there a way to do the following thing?
class A:
# do something
some_func(A)
# ...
推荐答案
在Python中,您不能在类主体中引用该类,尽管在Ruby之类的语言中也可以引用它。
In Python you cannot reference the class in the class body, although in languages like Ruby you can do it.
在Python中,您可以使用类装饰器,但在类初始化后将被调用。另一种方法是使用元类,但这取决于您要实现的目标。
In Python instead you can use a class decorator but that will be called once the class has initialized. Another way could be to use metaclass but it depends on what you are trying to achieve.
这篇关于python:如何从其中引用类(如递归函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文