Basic misunderstanding on object creation

Terry Reedy tjreedy at udel.edu
Wed May 13 12:36:09 EDT 2015


On 5/13/2015 9:25 AM, andrew cooke wrote:

> The following code worked on Python 3.2, but no longer works in 3.4.

Bugfixes break code that depends on buggy behavior. See
https://bugs.python.org/issue1683368
Your code also fails in 2.7.9 if you inherit Foo from object.
The exact error messages changed for 3.4 in
https://bugs.python.org/issue7963

 > Did something change,

Obviously yes.

 > or have I always been doing something dumb?

You were depending on behavior of object that Guido decided was buggy.

I found the tracker issue by looking for 'object' in the Core and 
Builtins sections of the changelog one can access from What's New, first 
paragraph (using Highlight All in Firefox).

>>>> 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.

-- 
Terry Jan Reedy




More information about the Python-list mailing list