I am trying to make an enum-type class in Python but it gets so lengthly when you have to do
VARIABLE1, VARIABLE2, VARIABLE3, VARIABLE3, VARIABLE4, VARIABLE5, VARIABLE6, VARIABLE7, VARIABLE8, ... , VARIABLE14 = range(14)
and I've tried to set it up like the following, but ended up not working.
VARIABLE1,
VARIABLE2,
VARIABLE3,
...
VARIABLE14 = range(14)
How would I accomplish this in the simplest way possible?
Enums can't have multiple value per name.
Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over. An enum has the following characteristics.
Python enumerations are classes. It means that you can add methods to them, or implement the dunder methods to customize their behaviors.
No. enum s are not designed to "wrap around" in the way you describe by default.
Oh, wow I just added brackets around the variables and it worked
(VARIABLE1,
VARIABLE2,
VARIABLE3,
...
VARIABLE14) = range(14)
Instead of manually typing VARIABLE1, VARIABLE2 ... you can do this:
>>> for x in range(1, 15):
globals()['VARIABLE{0}'.format(x)] = x
Does what you want, without the extra effort of typing VARIABLE1 ... VARIABLE 14.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With