When I use the Functional API to create an enum, I get back an enum object that allows arbitrary assignment (i.e. it has a __dict__):
e = enum.Enum('Things',[('foo',1),('bar',2)])
e.baz = 3
The item does not appear in the list:
list(e)
[<foo.foo: 1>, <foo.bar: 2>]
But it can still be referenced:
if thing == e.baz: ...
Now while it seems unlikely to ever occur, one of the reasons I want to use an enum is to prevent spelling mistakes and string literals, and for those things to be caught when a module is imported or as early as possible.
Is there a way to dynamically build an enum that behaves more like a __slots__ object that does not allow arbitrary attributes to be assigned?
from Python enum prevent invalid attribute assignment
No comments:
Post a Comment