Chạy mã này:TypeError tài sản classmethod: 'sở hữu' đối tượng không Iterable
import weakref
class A(object):
_instances = []
def __init__(self):
self._instances.append(weakref.ref(self))
@property
@classmethod
def instances(cls):
for inst_ref in cls._instances:
inst = inst_ref()
if inst is not None:
yield inst
foo = A()
bar = A()
for inst in A.instances:
print inst
tôi nhận được lỗi này:
Traceback (most recent call last):
File "test.py", line 18, in <module>
for inst in A.instances:
TypeError: 'property' object is not iterable
tôi không thể tìm ra cách có một phương pháp học cư xử như một thuộc tính (không có dấu ngoặc đơn).
- Bất cứ ai có thể giải thích cho tôi tại sao tôi gặp lỗi này?
- Bất cứ ai có thể giải thích cho tôi cách tôi có thể có phương thức lớp học hoạt động như một tài sản không?
Tôi sẽ đi với nó vì nó trông tao nhã nhất với tôi. Cảm ơn rất nhiều! (PS Tôi không thể upvote vì tôi nhỏ hơn 15, xin lỗi vì điều này ...). – Narann