Feb-19-2024, 03:51 PM
(This post was last modified: Feb-19-2024, 03:51 PM by deanhystad.)
Not quite. calculate_gas_milage() would try to call a function named calculate_gas_milage and return a result. This would either raise a NameError, or map "efficiency" to the result, not the function. More importantly, python knows that it needs to change instance.method(args) into insance.__class__.method(instance, args) when it is calling an instance method of a class. It would not know to do that if you just stuffed a function in a dictionary.
I'm occasionally tasked with maintaining Python projects that use dictionaries to mimic classes, even though Python always had classes. Let me just say that doing this is a bad idea and leave it at that. Shudder!
I'm occasionally tasked with maintaining Python projects that use dictionaries to mimic classes, even though Python always had classes. Let me just say that doing this is a bad idea and leave it at that. Shudder!