Quiz question regarding __init__ method | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Quiz question regarding __init__ method

I encountered confusing question in Python challenges: "Which of the following best describes the __init__ method of a class? - It's a class constructor. - It's a class destructor. - It's a property decorator. - It's an object initialization." with only one accepted answer: "It's a class constructor", but even after reviewing the question I was still more inclined to select "It's an object initialization". I looked it up in the Python documentation (https://docs.python.org/3/reference/datamodel.html) which states: __new__(cls[, ...]) is called to create a new instance of class. __init__(self[, ...]) is called after the instance has been created (by __new__()). __new__() and __init__() work together in constructing objects (__new__() to create it, and __init__() to customize it) So I consider __init__ to be just initializer called after the constructor __new__, because the instance object already exists when __init__ is called. What are your thoughts? Did anyone else find this question confusing?

27th Jan 2019, 3:54 PM
Radosław Chmielewski
3 Antworten
+ 2
Correct me if I'm wrong, but for me a "constructor" is a member function that is called automatically whenever an instance of the class is created. Which is what happens with __init__(self) in python. The method is called upon construction, it doesn't "construct" anything by itself.
27th Jan 2019, 4:07 PM
Anna
Anna - avatar
+ 2
That's true. If "constructor" wasn't one of the answers, I would have thought "object initialization" too.
27th Jan 2019, 6:00 PM
Anna
Anna - avatar
+ 1
Thank you for your clarification. In this case both answers ("class constructor" and "object initialization") are really close unless I miss something important here.
27th Jan 2019, 5:53 PM
Radosław Chmielewski