Basic misunderstanding on object creation

Ian Kelly ian.g.kelly at gmail.com
Wed May 13 10:54:28 EDT 2015


On Wed, May 13, 2015 at 8:42 AM, andrew cooke <andrew at acooke.org> wrote:
> 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.

__init__ is not called by __new__. In the object construction, __new__
is called, and *then* __init__ is called on the result.



More information about the Python-list mailing list