Python - 相同 class 的所有实例的重置值
Python - Reset value for all instances of the same class
在我正在编码的 Ren'Py 游戏中,我正在尝试优化我的代码,但我一直在寻找一种方法来为相同的所有实例重置相同的值 class.
这是我的代码:
init python:
class Girl():
def __init__(self,name,age):
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
Bree = Girl("Bree",26)
Sasha = Girl("Sasha",27)
label reset_appear():
Bree.appear = False
Sasha.appear = False
现在我只有几个实例 class,但我计划再添加 50 个,我想在继续之前修复它。
我想做这样的事情(还在class):
def reset_appear(self):
self.appear = False
但是您仍然需要为每个实例调用它。我也想到了 class 之外的相同功能,但我不知道如何编码。
您可以使用 class 属性来存储所有实例和 class 方法
遍历所有实例:
class Girl:
_instances = []
def __init__(self, name, age):
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
self._instances.append(self)
@classmethod
def reset_appear(cls):
for instance in cls._instances:
instance.appear = False
def __repr__(self):
# added for sake of example
return '{} {}'.format(self.name, self.appear)
Bree = Girl("Bree", 26)
Bree.appear = True
Sasha = Girl("Sasha", 27)
Girl.reset_appear()
print(Girl._instances)
# [Bree False, Sasha False]
我对Ren'Py不熟悉,所以我的例子都是正常的Python。
您需要在某个地方收集 Girl
class 的所有实例。
这可以是一个简单的列表:
class Girl():
def __init__(self,name,age):
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
Bree = Girl("Bree", 26)
Sasha = Girl("Sasha", 27)
girls = [Bree, Sasha]
for girl in girls:
girl.appear = False
或者您可以让 class 处理它:
class Girl():
instances = []
def __init__(self,name,age):
Girl.instances.append(self)
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
for girl in Girl.instances:
girl.appear = False
在我正在编码的 Ren'Py 游戏中,我正在尝试优化我的代码,但我一直在寻找一种方法来为相同的所有实例重置相同的值 class.
这是我的代码:
init python:
class Girl():
def __init__(self,name,age):
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
Bree = Girl("Bree",26)
Sasha = Girl("Sasha",27)
label reset_appear():
Bree.appear = False
Sasha.appear = False
现在我只有几个实例 class,但我计划再添加 50 个,我想在继续之前修复它。
我想做这样的事情(还在class):
def reset_appear(self):
self.appear = False
但是您仍然需要为每个实例调用它。我也想到了 class 之外的相同功能,但我不知道如何编码。
您可以使用 class 属性来存储所有实例和 class 方法 遍历所有实例:
class Girl:
_instances = []
def __init__(self, name, age):
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
self._instances.append(self)
@classmethod
def reset_appear(cls):
for instance in cls._instances:
instance.appear = False
def __repr__(self):
# added for sake of example
return '{} {}'.format(self.name, self.appear)
Bree = Girl("Bree", 26)
Bree.appear = True
Sasha = Girl("Sasha", 27)
Girl.reset_appear()
print(Girl._instances)
# [Bree False, Sasha False]
我对Ren'Py不熟悉,所以我的例子都是正常的Python。
您需要在某个地方收集 Girl
class 的所有实例。
这可以是一个简单的列表:
class Girl():
def __init__(self,name,age):
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
Bree = Girl("Bree", 26)
Sasha = Girl("Sasha", 27)
girls = [Bree, Sasha]
for girl in girls:
girl.appear = False
或者您可以让 class 处理它:
class Girl():
instances = []
def __init__(self,name,age):
Girl.instances.append(self)
self.name = name
self.age = age
self.place = "Nowhere"
self.appear = False
for girl in Girl.instances:
girl.appear = False