我当然试过:
class PdfContentRecord(NamedTuple):
filename: str
page: int
cache: dict
data: dict = dict()
accessed: str = None
def __new__(cls, *args, **kwargs):
self = super().__new__(*args, **kwargs)
self.accessed = datetime.now().isoformat()
return self
但是我得到了和How to provide additional initialization for a subclass of namedtuple?完全相同的错误
我不知道attrs
是否能帮助我(太难理解了)。dataclasses.dataclass
可能有帮助,但它只支持python3.7。在
或者我可以用__slots__
编写我的类。。。在
编辑:
Did you read the answer in the question you linked?
与from collection import namedtuple
一起工作,但不与{
python3.7+用户应该只利用^{} 或^{} 中的^{} 钩子。对于使用旧版Python的用户,请继续阅读。。在
我认为你应该可以用一层额外的类型来完成:
不过,可以说,您首先失去了使用
NamedTuple
的一些好处,而且还可以自己编写子类型。在一个非常漂亮的答案,需要
pip install attrs
(这激发了dataclass
)对于^{} 版本,不需要
^{pr2}$pip
,而是at least Python 3.6 is needed。在相关问题 更多 >
编程相关推荐