Tôi muốnPython: @staticmethod với @property
Stats.singleton.twitter_count += 1
và tôi nghĩ tôi có thể làm
class Stats:
singleton_object = None
@property
@staticmethod
def singleton():
if Stats.singleton_object:
return Stats.singleton_object
Stats.singleton_object = Stats()
return Stats.singleton()
Nhưng nó ném một ngoại lệ:
>>> Stats.singleton.a = "b"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'property' object has only read-only attributes (assign to .a)
nơi 'singleton_object' được xác định? – tonfa
ở đâu là 'self' được định nghĩa? – tonfa
xác định "không hoạt động" – u0b34a0f6ae