Basic misunderstanding on object creation

andrew cooke andrew at acooke.org
Wed May 13 10:42:15 EDT 2015


On Wednesday, 13 May 2015 11:36:12 UTC-3, Thomas Rachel  wrote:
> Am 13.05.2015 um 15:25 schrieb andrew cooke:
> 
> >>>> class Foo:
> > ...     def __new__(cls, *args, **kargs):
> > ...         print('new', args, kargs)
> > ...         super().__new__(cls, *args, **kargs)
> 
> > new (1,) {}
> > Traceback (most recent call last):
> >    File "<stdin>", line 1, in <module>
> >    File "<stdin>", line 4, in __new__
> > TypeError: object() takes no parameters
> 
> object's __new__() dosn't take any parameters. So call it without arguments:
> 
> class Foo:
>      def __new__(cls, *args, **kargs):
>          print('new', args, kargs)
>          super().__new__(cls)
> 
> (at least if we know that we inherit from object. Might be that this one 
> doesn't work very good with multiple inheritance...)
> 
> 
> Thomas

But then nothing will be passed to __init__ on the subclass.

Andrew



More information about the Python-list mailing list