的实例,我想我还没有undertood如何定义从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
此代码引发TypeError : __new__() takes exactly 4 arguments (2 given)
。
任何想法为什么?
__new__被__init__其被期待多个参数之前调用。 new和init需要有完全相同数量的参数。 – karthikr