__init__ is the initialiser

Ben Finney ben+python at benfinney.id.au
Sat Feb 1 00:47:42 CET 2014


Cameron Simpson <cs at zip.com.au> writes:

> > On 01/31/2014 12:48 PM, MRAB wrote:
> > >The advantage of calling it the "initialiser" is that it explains
> > >why it's called "__init__".
>
> On this basis, would it suffice to change the opening sentence from:
>   Called when the instance is created.
>
> to
>   Called to initialise a new instance immediately after creation.
>
> ?
>
> This seems succinct while getting both "initialise" and "new" into the
> line, which makes it clear that there is a separate and earlier "new"
> step. (Conveniently overridable with __new__ :-)

It leaves a naive reader (who isn't yet familiar with the convention for
special names in Python) with the false implication that “__init__” is
usually called *manually*.

I would prefer it to be clear that “__init__” is called automatically,
*during* the constructor's operation. So, instead of:

    Called when the instance is created.

I suggest:

    Called automatically by the constructor “__new__” during instance
    creation, to initialise the new instance.

-- 
 \     “Why doesn't Python warn that it's not 100% perfect? Are people |
  `\         just supposed to “know” this, magically?” —Mitya Sirenef, |
_o__)                                     comp.lang.python, 2012-12-27 |
Ben Finney




More information about the Python-list mailing list