Sir in class Car we are writing self.model = model and self.mileage=mileage but where we have created the variables inside the class? How can we just add value to them without creating them?
Where the variable model are created in class?
Hello @i.m.anand_31, yes we haven’t declared these variables inside the class. But if we see __init__
is the dunder or we can also say it as a constructor of the class. And in this __int__
function we have passed the variables mileage and model. And passing these variables into it declares the variables for the class. And then we can access them throughout the class. And while calling the function Car. Like we have done c1 = Car(‘a’,2) here we have passed the value ‘a’ for the model and 2 for the mileage. These are the values that we are passing to the constructor.
I hope it is clear to you. In case there is some doubt or issue pls let me know.
And if it is clear to you, pls mark it as resolve and pls feel free to provide the feedback.
can we use the variable declared inside init outside it , like from outside , using c1.model ?
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.