Python classes: "__init__()" method


#1

Excuse me, in this exercise I was wondering… when you assign the parameters to their names, why do you need to do this, and what if you do it another way, assigning something like “self.model = car_type” ? Can someone explain why you must do “self.model = model”, even if it gets a bit technical? Thank you!


#2

That would be equally as valid if it is the name of the parameter. It just makes better sense to readers if the instance variable has the same name as the parameter.

self.car_type = car_type

Now when we get into semantics, type could apply to a number of different aspects of a car such as, two-door vs. four-door vs. two-door-hatchback, and so on; or, gasoline (petrol) vs. diesel vs. propane vs. natural gas vs. electric; and so on. The list is long of how we catergorize and type a car.

Car makers and dealerships all refer to them by model so in this program, that seems the most appropriate, and semantic term to use.

Variable naming is not something we do on a whim. If our code is to be understood by another reader/coder then the names we use need to describe purpose and behavior as close as can be. Otherwise we need to document the heck out of it, which only adds to verbosity. The less our intentions are misunderstood, the better for all parties.


#3

If your object wants to hold on to that information it better put it somewhere. No such thing as “have to”, but there is opportunity