3 ms·
Seems a bit dodgy that enum values can be any value. What happens if you use mutable values and later change them to be the same?
by lmm 14y ago
Seems a bit dodgy that enum values can be any value. What happens if you use mutable values and later change them to be the same?
- dvanduzer 14y agoObjects can be mutable or immutable. The value of a mutable object can change to another value, but it's not that the value-in-itself is mutable. http://docs.python.org/3/reference/datamodel.html#objects-values-and-types http://docs.python.org/3/reference/datamodel.html#objects-va...
- lmm 14y agoSo can I declare an enum value to be e.g. a list? Is that value "frozen" when the enum declaration is processed or what?
- dvanduzer 14y agoA list is not a value, it is a type of object. The second to last paragraph of the section in the reference manual I linked may help: Some objects contain references to other objects; these are called containers. Examples of containers are tuples, lists and dictionaries. The references are part of a container’s value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container (like a tuple) contains a reference to a mutable object, its value changes if that mutable object is changed. PEP-435 says enums are bound to "unique, constant values" -- not that you can bind enums to arbitrary types of objects.
- pyre 14y agoYou could say that about a lot of things in Python. If you add a constant value as a class attribute, anyone using that class has the ability to change it, even if that would screw things up.