小编Séb*_*ent的帖子

无法初始化子类化的namedtuple的实例

我想我还没有考虑如何定义一个从namedtuple子类化的类:

from collections import namedtuple
PD = namedtuple('PD', 'x y z')
p1 = PD(0, 'u', 1)
print p1.x #<== this works

class PDsub(PD):
   __slots__ = ()
   def __new__(cls, x, y, z):
        self = super(PDsub, cls).__new__(cls, x, y, z)
        return self

   def __init__(self, a):
        self.x, self.y, self.z = a, a, a

   def __str__(self):
        return 'Foo'

p2 = PDsub(5) #<== this does not work
Run Code Online (Sandbox Code Playgroud)

这段代码提出来了TypeError : __new__() takes exactly 4 arguments (2 given).

有什么想法吗?

python namedtuple

4
推荐指数
1
解决办法
1198
查看次数

标签 统计

namedtuple ×1

python ×1