__init__ is the initialiser
Ben Finney
ben+python at benfinney.id.au
Fri Jan 31 18:05:07 EST 2014
Ned Batchelder <ned at nedbatchelder.com> writes:
> On 1/31/14 2:33 PM, Mark Lawrence wrote:
> > From http://docs.python.org/3/reference/datamodel.html#object.__init__
> > […]
> > Should the wording of the above be changed to clearly reflect that
> > we have an initialiser here and that __new__ is the constructor?
>
> I'm torn about that. The fact is, that for 95% of the reasons you want
> to say "constructor", the thing you're describing is __init__.
I assume the “95%” figure is made up. Whose wants are you divining, here?
> Most classes have __init__, only very very few have __new__.
Yes, this is true, and I agree it is a major factor in the confusion:
the term “constructor” is commonly encountered in discussion of classes,
and in Python, the method “__init__” is commonly encountered. It is a
natural mistake to think Python names its constructor “__init__”, since
most classes don't define “__new__”.
> The sense that __new__ is the constructor is the one borrowed from C++
> and Java: you don't have an instance of your type until the
> constructor has returned. This is why __init__ is not a constructor:
> the self passed into __init__ is already an object of your class.
A more salient reason, I think, is that “constructor” entails that the
function will return the instance. This is another important reason why
“__init__” is not a constructor: it does not return the instance.
> But that distinction isn't useful in most programs. The thing most
> people mean by "constructor" is "the method that gets invoked right at
> the beginning of the object's lifetime, where you can add code to
> initialize it properly." That describes __init__.
Here we disagree. I think the meaning “… and that returns the new
instance” is entailed in the meaning of “constructor”.
> Why can't we call __init__ the constructor and __new__ the allocator?
Because those terms already have meanings, and “__new__” fits the
meaning of “constructor” better.
--
\ “This world in arms is not spending money alone. It is spending |
`\ the sweat of its laborers, the genius of its scientists, the |
_o__) hopes of its children.” —Dwight Eisenhower, 1953-04-16 |
Ben Finney
More information about the Python-list
mailing list