我刚刚发现attrs
,它实际上对我即将进行的项目非常有用。在玩耍时,我发现了一些我无法向自己解释的行为。
我有这段代码:
from attr import attrs, attrib, Factory
def validate_dict(instance, attribute, value):
if not isinstance(value, dict):
raise ValueError(f"Attribute `{attribute.name}` has to be of type dict(), not {type(value)}!")
@attrs(kw_only=True, on_setattr=validate_dict)
class FooBar:
defaults = attrib(default=Factory(dict), validator=validate_dict)
config = attrib(default=Factory(dict), validator=validate_dict)
source = attrib(default=Factory(dict), validator=validate_dict)
target = attrib(default=Factory(dict), validator=validate_dict)
>>> foobar = FooBar()
>>> foobar
FooBar(defaults={}, config={}, source={}, target={})
>>> foobar.defaults = {'firstname':'Thomas'}
>>> foobar
FooBar(defaults=None, config={}, source={}, target={})
使用foobar.defaults.update(firstname='Thomas')
也可以foobar = FooBar(defaults={'firstname':'Thomas'})
,但不应该直接分配吗?还是我使用的方式on_setattr
不对validator
?
当我尝试使用 int 设置属性时,即foobar.defaults = 1
正确引发了 ValueError。
任何提示表示赞赏。问候,托马斯