(Feb-19-2024, 02:21 PM)Pedroski55 Wrote: I read that a class is basically a dictionary. I believe the value of a dictionary key:value pair can be a function.Now is classes💪 is lot more that just a dictionary,it store values values from
So in the case of class Car() do we have:
Car = {"color": 'blue', "mileage": 5, "fuel_efficiency": 14.5, "efficiency": calculate_gas_usage() }
__init__
internally.Can look at this:
>>> my_car = Car('blue', 8.8, 10.5) >>> my_car.__dict__ {'color': 'blue', 'fuel_efficiency': 10.5, 'mileage': 8.8} >>> my_car.__dict__['color'] 'blue'The
calculate_gas_usage
method is not stored in this dictionary.Instead, it's a part of the class definition and is accessed through the class's namespace.
When you call
my_car.calculate_gas_usage(distance)
,Python automatically passes
my_car
as the first argument to the calculate_gas_usage
method.>>> my_car.calculate_gas_usage(275.5) 26.238095238095237