我想创建一组类来管理不同实验的不同配置参数。我想为每个类设置一个属性列表作为class属性,以检查给定的属性是否确实需要。你知道吗
为了保存代码,我编写了一个通用的__init__
,希望它可以应用于派生类,并使用派生类的_attr_
来执行检查工作。你知道吗
我使用__class__
来引用当前类,但它似乎指向基类。你知道吗
这里有一些代码。BCDConfig固有的__init__
函数坚持_class__
应该是在--ExpConfig中定义它的类。你知道吗
import json
class ExpConfig:
_attr_ = ['attr1', 'attr2'] # list of string
def __init__(self, config):
self.config = {}
# only take defined attributes
for key in config:
if key in __class__._attr_:
self.config[key] = config[key]
else:
raise ValueError
# check if there is anything undefined
for key in __class__._attr_:
assert key in self.config, "Missing arguments!"
def save_config(self, path):
with open(path, 'w') as f:
json.dump(self.config, f)
@classmethod
def load_config(cls, path):
with open(path, 'r') as f:
config = json.load(f)
exp_config = __class__(config)
return exp_config
class BCDConfig(ExpConfig):
_attr_ = ['attr1', 'attr2', 'attr3']
def __init__(self, config):
super(BCDConfig, self).__init__(config)
if __name__ == '__main__':
bcd_config1 = BCDConfig({'attr1':123, 'attr2':444})
bcd_config1.save_config('./bcd1.cfg')
print(BCDConfig.load_config('./bcd1.cfg').config)
bcd_config2 = BCDConfig({'attr1':1253, 'attr2':4344, 'attr3':1})
bcd_config2.save_config('./bcd2.cfg')
print(BCDConfig.load_config('./bcd2.cfg'))
这是输出。我想知道除了__class__
之外,是否还有其他方法可以动态地解释到派生类。谢谢你的帮助!你知道吗
{'attr1': 123, 'attr2': 444}
Traceback (most recent call last):
File "C:/Users/MysriO/Documents/Local Codes/DEAP_Ricotta/exp_config.py", line 46, in <module>
bcd_config2 = BCDConfig({'attr1':1253, 'attr2':4344, 'attr3':1})
File "C:/Users/MysriO/Documents/Local Codes/DEAP_Ricotta/exp_config.py", line 37, in __init__
super(BCDConfig, self).__init__(config)
File "C:/Users/MysriO/Documents/Local Codes/DEAP_Ricotta/exp_config.py", line 14, in __init__
raise ValueError
ValueError
__class__
只指向在其上定义方法的类。它的目的是不随子类而改变。你知道吗如果要获取当前实例的类,请使用^{} function (例如
type(self)
)。在本例中返回self.__class__
,但是要知道type()
知道如何处理不同类型的对象,而不仅仅是Python类。可能你一直想用self.__class__
。你知道吗我不会使用
__class__
,除非您特别希望访问定义了方法的类对象,忽略继承,然后只使用显式注释来解释为什么要这样做。__class__
闭包并不广为人知,也不打算用于一般用途。你知道吗从reference documentation on class creation:
对于
load_config
类方法,已经有了对类的引用:在那里使用cls
,而不是__class__
。你知道吗接下来,您实际上不需要在
__init__
中使用类引用。您可以改用self._attr_
;类属性可以通过实例访问,前提是没有实例属性对它们进行隐藏:self._attr_
引用将在给定实例的正确类上找到_attr_
属性:实际上我会把
_attr_
做成set object,而不是一个列表。属性名称必须是唯一的,不需要特定的顺序,并且集合针对成员资格测试和交集进行了优化。如果将集合与dictionary views组合,则可以快速测试缺少的键和无关键:我使用了一个
frozenset()
对象,一个不可变的集合,因为您可能不想在创建类之后更改属性名。一个frozenset()
将保护您不受意外错误的影响。你知道吗最后,子类可以使用set union operator ^{} 重用父类的定义,因此
BCDConfig
可以定义为:相关问题 更多 >
编程相关推荐