以下面的类和一个类实例为例:
class NewClass:
def __init__(self, a):
self.number = a
def func(self):
return
@property
def test_property(self):
return self.test_property
new = NewClass(1)
尝试获取属性和方法的类型:
>>> print(type(new.number))
int
>>> print(type(new.func))
method
获取@property
的类型会导致递归错误:
>>> print(type(new.test_property))
: maximum recursion depth exceeded while calling a Python object
return.RecursionError
如何获取@property
属性的类型?
答案 0 :(得分:3)
也许你打算像这样定义你的财产:
class NewClass:
def __init__(self, a):
self.number = a
def func(self):
return
@property
def test_property(self):
#return self.test_property # Always going to call itself forever
return self.number # Maybe this is what you meant?