What is the difference between "new Class()" and "Class()"?

Alan Gauld alan.gauld at btinternet.com
Sun Nov 20 20:28:14 EST 2011


On 20/11/11 21:16, Herman wrote:
> What is so special about the "new" operator?

Assuming you mean the __new__() method?

__new__() gets called when the class is being constructed, it returns 
the new instance. It is the real Pyhon constructor.

As opposed to __init__() which is called after the instance has been 
created. It is not really a constructor but an initializer. (Even though 
most of us treat it as a constructor!)

But you should never see code like

meat = new Spam().

it should be

meat = Spam()

Which calls __new__() followed by __init__().

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Python-list mailing list