In python we can access class and object directly without using/initialize __init__ special method? Is __init__ same also a constructor?
            Asked
            
        
        
            Active
            
        
            Viewed 144 times
        
    4
            
            
        
        martineau
        
- 119,623
 - 25
 - 170
 - 301
 
        Abdul Wahab
        
- 49
 - 5
 
- 
                    1Yes, it's similar to constructors in other languages. – Barmar Jul 02 '21 at 20:28
 - 
                    1(1) Yes, (2) no. Was there a particular problem with something you tried? – mkrieger1 Jul 02 '21 at 20:28
 - 
                    2It's optional if you don't need to initialize anything when the new instance is created. – Barmar Jul 02 '21 at 20:28
 - 
                    This topic has been already picked up so many times on StackOverflow. I think this is the most popular: https://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes – blazej Jul 02 '21 at 20:56
 
1 Answers
1
            
            
        In fact it is not a constructor. As it's name says, it's "initializer". In python __new__ constructs the new instances and then __init__ initializes it.
If you don't want to do initialization, you don't need to implement it.
        S.B
        
- 13,077
 - 10
 - 22
 - 49
 
- 
                    How exactly does this differ from constructors in, say, Java, which also initialise newly-created objects? Wikipedia's article on [constructors](https://en.wikipedia.org/wiki/Constructor_(object-oriented_programming)) says, for example, *"They have the task of initializing the object's data members"*; do you have a source which says that is not the job of a constructor? – kaya3 Jul 02 '21 at 23:06
 - 
                    @kaya3 unfortunately I don't know about what's going on in Java. In python `__new__` has the duty of creating new instance object. Of course you can interpret that and do your "initialization" there if you want and skip the `__init__` method totally. But mostly we do initialization in `__init__`. As a proof, Try to implement `__new__` method and return something else like `10`. Then the `__init__` won't get called at all. `__init__` gets called exactly after the `__new__` method created the object (if the conditions are present). – S.B Jul 03 '21 at 10:35
 - 
                    In Java, the `new` keyword creates the object, and a constructor initialises the new object, just like Python's `__init__`. I've heard it said that, for this reason, Java's constructors are not really constructors, they are initialisers - but this doesn't seem to agree with the way the word "constructor" is used by authoritative sources, so I'm wondering if you know of a textbook or other source which does distinguish between constructors vs. initialisers as general, language-independent terms (as opposed to in a specific language like C#). – kaya3 Jul 03 '21 at 10:40
 - 
                    Compare for example, Javascript's so-called "arrays" which are really lists, and PHP's so-called "arrays" which are really maps or associative arrays. Those languages use the word "array" in a non-standard way compared to how authoritative sources define the word "array", so it's entirely possible there's a standard by which Java's "constructors" are not truly constructors; I just don't know of one. – kaya3 Jul 03 '21 at 10:43
 - 
                    @kaya3 https://docs.python.org/3/reference/datamodel.html#object.__new__ It uses the word constructor for `__new__` and initializer for `__init__` – S.B Jul 03 '21 at 10:49
 - 
                    I'm not saying `__init__` does create a new instance or that `__new__` doesn't. I'm saying that creating a new instance isn't required for something to be called a constructor, at least in popular languages like Java (and many others). – kaya3 Jul 03 '21 at 10:51
 - 
                    @kaya3 maybe that's because i'm strange with other languages. Construction for me implies the "creation" of something new. – S.B Jul 03 '21 at 10:56