Multiple inheritance, super() and changing signature

Steven D'Aprano steve at pearwood.info
Thu Jun 2 13:36:14 EDT 2016


On Thu, 2 Jun 2016 06:22 pm, Lawrence D’Oliveiro wrote:

> On Wednesday, June 1, 2016 at 8:02:14 AM UTC+12, Ben Finney wrote:
>> (Note that ‘__init__’ is not a constructor, because it operates on the
>> *already constructed* instance, and does not return anything.
> 
> Believe it or not, that *is* what “constructor” means in every OO
> language. 

I don't believe it. 

C# is an OO language, and it distinguishes constructors and initialisers:

https://msdn.microsoft.com/en-us/library/bb397680.aspx

(although they don't seem to mean quite the same as what they mean in
Python).


Objective C is also an OO language, and it calls `init` the initializer:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/index.html#//apple_ref/occ/instm/NSObject/init

The documentation doesn't specifically give a name to the `alloc` method,
but since it allocates memory for the instance, "allocator" is the obvious
term.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/index.html#//apple_ref/occ/clm/NSObject/alloc

And `new` merely calls alloc, then init. (Although I'm lead to understand
that in older versions of Cocoa, `new` handled both allocation and
initialisation.)

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/index.html#//apple_ref/occ/clm/NSObject/new

Smalltalk, the grand-daddy of OOP languages, doesn't even have constructors
(at least not in the modern OOP sense):

http://www.slideshare.net/SmalltalkWorld/stoop-008fun-withsmalltalkmodel
http://image.slidesharecdn.com/stoop-008-funwithsmalltalkmodel-101025144609-phpapp02/95/8-oop-smalltalk-model-3-638.jpg?cb=1422578644

Any method can be used to create an object, there is no reserved name for
such a method.

Python is an OO language, what does it say?

The glossary doesn't define *either* constructor or initialiser (or
initializer):

https://docs.python.org/3/glossary.html

and the docs for __new__ and __init__ don't refer to them by either name:

https://docs.python.org/3/reference/datamodel.html#basic-customization

The docs do refer to the "object constructor expression", but that's the
call to the class, not the special method. And the term "initialiser"
or "initializer" is frequently used by Python developers to refer to
__init__:

https://rosettacode.org/wiki/Classes#Python


> Technically it should be called the “initializer”, but 
> “constructor” is the accepted term for the special method that is called
> to initialize a newly-allocated class instance.

Not in Python circles it isn't.

But since the constructor/initialiser methods are so closely linked, many
people are satisfied to speak loosely and refer to "the constructor" as
either, unless they specifically wish to distinguish between __new__ and
__init__.



-- 
Steven



More information about the Python-list mailing list