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
-
1Yes, it's similar to constructors in other languages.Barmar– Barmar2021年07月02日 20:28:17 +00:00Commented Jul 2, 2021 at 20:28
-
1(1) Yes, (2) no. Was there a particular problem with something you tried?mkrieger1– mkrieger12021年07月02日 20:28:37 +00:00Commented Jul 2, 2021 at 20:28
-
2It's optional if you don't need to initialize anything when the new instance is created.Barmar– Barmar2021年07月02日 20:28:40 +00:00Commented 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/…blazej– blazej2021年07月02日 20:56:45 +00:00Commented Jul 2, 2021 at 20:56
1 Answer 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
S.B
17.1k12 gold badges38 silver badges74 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
kaya3
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?
S.B
@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).kaya3
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
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.
S.B
@kaya3 docs.python.org/3/reference/datamodel.html#object.__new__ It uses the word constructor for
__new__ and initializer for __init__ |
lang-py