[Tutor] When to use __new__ vs. __init__ ?

wesley chun wescpy at gmail.com
Thu May 21 03:36:13 EDT 2020


On Fri, May 1, 2020 at 2:09 PM tutor <tutor at python.org> wrote:

> On 30/04/20 11:52 AM, wesley chun wrote:
> > I concur with both Mats' and Alan's responses, and add that more
> > explicitly, __new__() is really only used for subclassing existing (user
> or
> > built-in) *immutable* types, not all Python built-in types. (If you
> > subclass dict or list, you don't need __new__() ). (Although, subclassing
> > mutable types have their own issues
> > <
> https://treyhunner.com/2019/04/why-you-shouldnt-inherit-from-list-and-dict-in-python/
> >
> > .)
> >
> > With traditional object-oriented languages, you have constructors (and
> > destructors) whose job it is to allocate/create (and deallocate/destroy)
> > objects. Python works differently in that the *Python interpreter*
> manages
> > your memory, i.e., it does the creation (and destruction) of regular
> > objects. That's why __init__() is named as an "initializer" more than a
> > constructor.
> >
> > For immutables, it's different because you need to be able to specify
> what
> > you want in such an object *before* it's created, so __new__() serves
> that
> > purpose... to specify its setup and then let Python create that object.
> For
> > normal/mutable objects, Python creates the object first, then lets you
> > initialize/customize it with __init__() before it's returned to be used
> by
> > your application. You'd only pick one (normal/mutable/__init__ or
> immutable/
> > __new__) and never both.
>
>
> "Never both"?
>
> Given that __new__ has a different purpose to __init__ it seems
> reasonable that one might use both, on the face of it.
>
> Is the "never" a Python law, or best-practice/good advice?
> (cannot recall ever trying both!)
> --
> Regards =dn
>

I'm only speaking for myself, but I've not seen that pattern after 20 years
of Python coding in production. Yes, you can physically do it, such as in this
SO Q&A <http://stackoverflow.com/questions/674304>, but in practice, not
that I've seen. The thing is why would you *need* both? If you're creating
an immutable object, it only makes sense to setup your object in `__new__`
because once it's created, you can't do anything about it. OTOH (on the
other hand), you'd typically use `__init__` to setup instance attributes,
and you'd only do that for mutable objects.

Even if you *could* use both (let's assume you can do setup before and
after an object is instantiated), you might as well put all of that code in
one or the other so as to avoid 2 function calls when creating an object.
It just doesn't make sense to me. Now, I haven't written large Python
systems in years, so I suppose there may be some use case out there, but
it's something I haven't seen before.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A computer never does what you want... only what you tell it."
    +wesley chun : wescpy at gmail : @wescpy
    Python training & consulting : http://CyberwebConsulting.com
    "Core Python" books : http://CorePython.com
    Python blog: http://wescpy.blogspot.com


More information about the Tutor mailing list