[Tutor] __init__

monikajg at netzero.net monikajg at netzero.net
Wed Aug 31 13:59:25 EDT 2016


Thank you very much. This was very helpful.

---------- Original Message ----------
From: Steven D'Aprano <steve at pearwood.info>
To: tutor at python.org
Subject: Re: [Tutor] __init__
Date: Wed, 31 Aug 2016 22:39:33 +1000

On Wed, Aug 31, 2016 at 04:05:26AM +0000, monikajg at netzero.net wrote:

> Somebody in this line of emails mentioned that python provides default 
> __init__ if it its not stated in the class by the programmer. And that 
> it is called by __new__

That may have been me, except I didn't mention __new__.

> Then later on you corrected that.

Instantiation of classes is slightly different in Python 2 and 3. In 
Python 3, there is only one mechanism. When you have a class, and you 
create an instance: 

    instance = MyClass(args)

the following happens:

    instance = MyClass.__new__(args)
    instance.__init__(args)


That is a simplified description, the actual details are a bit more 
complicated, but if you think of it like the above, you will be okay 
except for the most unusual situations.

Does this mean that all classes must define __new__ and __init__? No! If 
you don't define them, your class will inherit them from the root of the 
class hierarchy, "object". object has those methods built-in:

py> object.__new__
<built-in method __new__ of type object at 0x81d9c20>
py> object.__init__
<slot wrapper '__init__' of 'object' objects>

and if you don't override one or both, you get the superclass method and 
its default behaviour. object.__new__ actually creates the instance, and 
object.__init__ takes no arguments and does nothing.

In Python 3, writing:

    class MyClass(object):

or just

    class MyClass:

has the same effect. Both cases inherit from object.


Things are a bit more complicated in Python 2. If you inherit from 
object, or from some other type that inherits from object (such as int, 
float, list, dict etc) then it behaves just like Python 3. But if you 
don't inherit from object, if you write:

    class MyClass:

with no parent classes listed, you get the legacy Python 1 behaviour, 
so-called "old-style" or "classic" classes.

You probably don't want to use classic classes. All sorts of things 
don't work with them, such as super, property, classmethod, 
staticmethod, and __slots__. Avoid classic classes unless you know what 
you are doing.

In a classic class, there is no __new__ method. Only __init__ is called. 
If you don't write an __init__ method, Python performs a default 
initialisation, but there's no method involved. The interpreter has the 
behaviour hard-coded, unless you override it.

So for both Python 3 "new-style" classes, and Python 2 "old-style" 
classes, writing __init__ in optional. If you don't write it, you get 
the default behaviour.




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

____________________________________________________________
Health News 24 (Sponsored by Content.Ad)
Don't Use Botox, Use This Instead: Granny Reveals $39 Method
http://thirdpartyoffers.netzero.net/TGL3241/57c71b3ecf4711b3e1670st02duc


More information about the Tutor mailing list