Why constructor name is same as class name ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why constructor name is same as class name ?

22nd Jul 2017, 10:42 AM
Master Gaurav
Master Gaurav - avatar
5 Answers
+ 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 :)
22nd Jul 2017, 10:50 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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.
22nd Jul 2017, 2:13 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
@clement Don't worry about it. I get my comments and answers downvoted, too. ;)
22nd Jul 2017, 2:35 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
@Kuba tralala Yes I just want to precise. ps - thq for the two downvotes
22nd Jul 2017, 2:20 PM
clement
clement - avatar
- 2
@Kuba Siekiertralala In python __new__ is the constructor.
22nd Jul 2017, 11:31 AM
clement
clement - avatar