[英]call a static method or property in __init__ in python
我有很多问题! 看下面的代码plz:
class Dog(object):
_the_most_oldest = int()
def __init__(self,age):
self.age=age
@staticmethod
@property
def the_most_oldest(age):
if Dog._the_most_oldest < age:
Dog._the_most_oldest=age
return Dog._the_most_oldest
属性是否可能是静态方法? 因为我需要一个在实例之间共享的静态变量-> _the_most_oldest dog! =>所以我需要@property。 并且因为the_most_oldest(age)
方法不适用于任何特殊实例,所以我需要@staticmethod!
我需要做的第二件事是在每个实例中调用the_most_oldest shuold并计算和刷新_the_most_oldest变量。 怎么办? 这有错误:
def __init__(self,age):
self.age=age
the_most_oldest(self.age)
_the_most_oldest = 0
(也许可以将其_the_oldest
,更好的英语) @staticmethod
只是一个“包含在类中”的函数,我认为使用@classmethod
会更好。 def _get_oldest(self):
return self._the_oldest
def _set_oldest(self, age):
if Dog._the_oldest < age:
Dog._the_oldest=age
the_oldest = property(_get_oldest, _set_oldest)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.