extend Python namedtuple with many @properties?

前端 未结 3 1672
甜味超标
甜味超标 2021-02-05 21:07

How can namedtuples be extended or subclassed with many additional @properties ?
For a few, one can just write the text below; but there are many, so I\'m looking for a gene

3条回答
  •  [愿得一人]
    2021-02-05 21:42

    The answer to your question

    How can namedtuples be extended or subclassed with additional @properties ?

    is: exactly the way you're doing it! What error are you getting? To see a simpler case,

    >>> class x(collections.namedtuple('y', 'a b c')):
    ...   @property
    ...   def d(self): return 23
    ... 
    >>> a=x(1, 2, 3)
    >>> a.d
    23
    >>> 
    

提交回复
热议问题