What arguments are passed to the __new__ method ?

Ian Kelly ian.g.kelly at gmail.com
Wed Mar 2 13:26:30 EST 2016


On Wed, Mar 2, 2016 at 10:57 AM, Rob Gaddi
<rgaddi at highlandtechnology.invalid> wrote:
> Peter Pearson wrote:
>
>> On Tue, 1 Mar 2016 18:24:12 +0100, ast wrote:
>>>
>>> It's not clear to me what arguments are passed to the
>>> __new__ method. Here is a piece of code:
>>>
>>>
>>> class Premiere:
>>>
>>>     def __new__(cls, price):
>>>          return object.__new__(cls)
>>>
>>>     def __init__(self, price):
>>>             pass
>> [snip]
>>
>> Of course, maybe you don't need to define a __new__ method at all.
>> Personally, I find that __init__ suffices for my simple needs.
>>
>
> I tend to need __init__ on about half of the classes I write.  I think
> I've needed __new__ all of twice in the years I've been writing Python.

Typically there are only two reasons to override __new__: you
potentially want to return an object of a different class than the
class that was called, or you're subclassing an immutable type and
need to handle the superclass arguments before they get passed to the
constructor.



More information about the Python-list mailing list