+ 2
Why constructor name is same as class name ?
5 Antworten
+ 9
Speaking for Python, it's not entirely precise. The constructor is always the magic method __init__ and it is called when you initialize (create) an instance of a class - an object.
But you are kind of right, too - you don't call the class's __init__ method, just the class itself, when constructing the new object :)
+ 6
@clement Technically, you are of course correct. But you usually do not define the __new__ method unless you really specifically need to. __init__ is the instance-oriented method, while __new__ is a static class method.
+ 5
@clement Don't worry about it. I get my comments and answers downvoted, too. ;)
+ 2
@Kuba tralala Yes I just want to precise.
ps - thq for the two downvotes
- 2
@Kuba Siekiertralala In python __new__ is the constructor.