Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot init an instance of a subclassed namedtuple

I think I have not undertood how to define a class subclassed from a 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

This code raises TypeError : __new__() takes exactly 4 arguments (2 given).

Any ideas why?

like image 944
Sébastien Vincent Avatar asked Feb 19 '23 23:02

Sébastien Vincent


1 Answers

Both the instance constructor (__new__) and your instance initializer (__init__) need to accept the same number of arguments.

Your __new__ needs 4 arguments, but your __init__ method only accepts 2. Adjust one or the other to accept the same number, or use a *args catch-all argument in your __init__ method.

For example, using the following __new__ method would make things work:

def __new__(cls, a):
    self = super(PDsub, cls).__new__(cls, a, a, a)
    return self

in which case you no longer need your __init__ initializer at all.

Demo:

>>> from collections import namedtuple
>>> PD = namedtuple('PD', 'x y 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 immutable type like a tuple often uses a __new__ constructor instead of a __init__ initializer; all the built-in immutables (frozenset, str, tuple) do so.

like image 182
Martijn Pieters Avatar answered Feb 27 '23 09:02

Martijn Pieters