4

In python we can access class and object directly without using/initialize __init__ special method? Is __init__ same also a constructor?

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jul 2, 2021 at 20:25
4
  • 1
    Yes, it's similar to constructors in other languages. Commented Jul 2, 2021 at 20:28
  • 1
    (1) Yes, (2) no. Was there a particular problem with something you tried? Commented Jul 2, 2021 at 20:28
  • 2
    It's optional if you don't need to initialize anything when the new instance is created. Commented Jul 2, 2021 at 20:28
  • This topic has been already picked up so many times on StackOverflow. I think this is the most popular: stackoverflow.com/questions/8609153/… Commented Jul 2, 2021 at 20:56

1 Answer 1

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.

answered Jul 2, 2021 at 21:17
Sign up to request clarification or add additional context in comments.

7 Comments

How exactly does this differ from constructors in, say, Java, which also initialise newly-created objects? Wikipedia's article on constructors 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 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).
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#).
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 docs.python.org/3/reference/datamodel.html#object.__new__ It uses the word constructor for __new__ and initializer for __init__
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.