假设我想正确地使用typing模块的命名元组类型注释:
from typing import NamedTuple, List
class Foo(NamedTuple):
my_list: List[int] = []
foo1 = Foo()
foo1.my_list.append(42)
foo2 = Foo()
print(foo2.my_list) # prints [42]
什么是Python中避免可变默认值错误的最佳或最干净的方法?我有一些想法,但似乎没有什么好的解决方案。
Using
None
as defaultclass Foo(NamedTuple): my_list: Optional[List[int]] = None foo1 = Foo() if foo1.my_list is None foo1 = foo1._replace(my_list=[]) # super ugly foo1.my_list.append(42)
Overwriting
__new__
or__init__
won't work:AttributeError: Cannot overwrite NamedTuple attribute __init__ AttributeError: Cannot overwrite NamedTuple attribute __new__
Special
@classmethod
class Foo(NamedTuple): my_list: List[int] = [] @classmethod def use_me_instead(cls, my_list=None): if not my_list: my_list = [] return cls(my_list) foo1 = Foo.use_me_instead() foo1.my_list.append(42) # works!
Maybe using
frozenset
and avoid mutable attributes altogether? But that won't work withDict
s as there are nofrozendict
s.
有没有人有一个好的答案?
classmethod
解决方案有任何问题,这也是我可能会选择的方法 - 尽管我会在类的主体中去掉默认值,以便默认值只能通过classmethod
来实现。 - Alex Waygoodclassmethod
的方法是危险的,因为我们仍然让实例化 Foo 的标准方式存在缺陷。但我能理解你的观点。 - Sebastian Wagner