Both instance constructors ( __new__ ) and your instance initializer ( __init__ ) must accept the same number of arguments.
Your __new__ requires 4 arguments, but your __init__ method accepts only 2. Configure one or the other to accept the same number, or use the *args catch-all argument in your __init__ method.
For example, using the following __new__ method __new__ make everything work:
def __new__(cls, a): self = super(PDsub, cls).__new__(cls, a, a, a) return self
in this case, you no longer need your __init__ initializer.
Demo:
>>> from collections import namedtuple >>> PD = namedtuple('PD', 'xy z') >>> class PDsub(PD): ... __slots__ = () ... def __new__(cls, a): ... self = super(PDsub, cls).__new__(cls, a, a, a) ... return self ... def __str__(self): ... return 'Foo' ... >>> p2 = PDsub(5) >>> p2.x, p2.y, p2.z (5, 5, 5) >>> str(p2) 'Foo'
An optional tuple-like type often uses the __new__ constructor instead of the __init__ initializer; all built-in constants ( frozenset , str , tuple ) do this.
Martijn pieters
source share