我只想在下面创建的类中重置'self.news'变量或获取新值,但是当我运行这三个函数时。所有值都返回其原始状态。并且以前使用的功能无效。如何仅更改或重置变量“ self.nexts”。非常非常的谢谢。
class new:
def __init__(self, name=None, nexts=None):
self.nexts = nexts
self.name = name
def one(self):
self.nexts = 2
return self.nexts
def two(self):
self.nexts = 1
self.name = 'What'
return self.nexts
def three(self):
self.__init__()
print(self.name)
print(self.nexts)
a = new()
print(a.one())
#output = 2
print(a.two())
#output = 1, hate
a.three()
#output = None, None
您可以通过以下方法初始化想要将实例变量重置为的变量:如何从成员函数中调用构造函数
def three(self):
self.__init__(None, self.nexts)
print(self.name)
print(self.nexts)
a.three()
# Output: None, 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句