[Tutor] __init__

Alan Gauld alan.gauld at yahoo.co.uk
Tue Aug 30 05:09:55 EDT 2016


On 29/08/16 23:52, monikajg at netzero.net wrote:
> I cannot really try it.

> If I have a class without __init__ and the class does not
 > inherit from a class that has init there is really no place
 > for me to put print statement.

Fair enough but in that case there is no __init__ to call.
The top level class object has an empty __init__() which
does nothing, so it will be called by __new__()

 > IN Java if you do not have a constructor specified java
 > calls a default constructor behind the scenes setting up memory.

Remember that init() is an initialiser, not a constructor.
The constructor is the rarely seen __new__() method. It is
new() that sets up the memory then calls init(). So init is
only used to initialise the object after it has been
constructed.

> Does python call default __init__ if one is not defined?

There is always one defined in object but it does nothing.

There is also a default new() in object which is what sets
up the memory etc and then calls init()

Alan G



More information about the Tutor mailing list