Say I want to create a class for car, tractor and boat. All these classes have an instance of engine and I want to keep track of all the engines in a single list. If I understand correctly if the motor object is mutable i can store it as an attribute of car and also the same instance in a list.
I cant track down any solid info on whether user defined classes are mutable and if there is a choice to choose when you define them, can anybody shed some light?
User classes are considered mutable. Python doesn’t have (absolutely) private attributes, so you can always change a class by reaching into the internals.
For using your class as a key in a
dictor storing them in aset, you can define a.__hash__()method and a.__eq__()method, making a promise that your class is immutable. You generally design your class API to not mutate the internal state after creation in such cases.For example, if your engines are uniquely defined by their id, you can use that as the basis of your hash:
Now you can use instances of class Engine in sets:
In the above sample I add another
Engine(1)instance to the set, but it is recognized as already present and the set didn’t change.Note that as far as lists are concerned, the
.__eq__()implementation is the important one; lists don’t care if an object is mutable or not, but with the.__eq__()method in place you can test if a given engine is already in a list: