[Tutor] __init__

monikajg at netzero.net monikajg at netzero.net
Tue Aug 30 12:59:43 EDT 2016


Thank you all for your answers.
The below seems contradicting to me:
" 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__()"

"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."
"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()"

so a class has a default, empty __init__ and __new__. __new__ sets up memory and calls __init__. 
In class that has no parent: __init is called by __new__ but really does not do anything since it is empty.

Assume you have a child class with no init, but it has empty, default __init__ provided by pathon and the same for its parent class. When instantiating child class, child class's __new__ calls its ___init__ in child class and then calls __init__ in parent class? Why does it not just use the default, provided by python __init__ since it found it?

---------- Original Message ----------
From: Alan Gauld via Tutor <tutor at python.org>
To: tutor at python.org
Subject: Re: [Tutor] __init__
Date: Tue, 30 Aug 2016 10:09:55 +0100

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

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


____________________________________________________________
Affordable Wireless Plans
Set up is easy. Get online in minutes.
Starting at only $14.95 per month! 
www.netzero.net?refcd=nzmem0216


More information about the Tutor mailing list