Basic misunderstanding on object creation

andrew cooke andrew at acooke.org
Wed May 13 09:25:01 EDT 2015


Hi,

The following code worked on Python 3.2, but no longer works in 3.4.  Did something change, or have I always been doing something dumb?

(I realise the code is pointless as is - it's the simplest example I can give of a problem I am seeing with more complex code).

>>> class Foo:
...     def __new__(cls, *args, **kargs):
...         print('new', args, kargs)
...         super().__new__(cls, *args, **kargs)
... 
>>> class Bar(Foo):
...     def __init__(self, a):
...         print('init', a)
... 
>>> Bar(1)
new (1,) {}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __new__
TypeError: object() takes no parameters

What I was expecting to happen (and what happens in 3.2) is that the object.__new__ method passes the argument to the __init__ of the subclass.

Any help appreciated.

Thanks,
Andrew



More information about the Python-list mailing list