Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Multi-lined Artificial enums using range

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?

like image 742
daxvena Avatar asked Dec 22 '10 18:12

daxvena


People also ask

Can enum have multiple values Python?

Enums can't have multiple value per name.

What are enumerations in Python?

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.

Can Python enums have methods?

Python enumerations are classes. It means that you can add methods to them, or implement the dunder methods to customize their behaviors.

Do enums wrap around?

No. enum s are not designed to "wrap around" in the way you describe by default.


2 Answers

Oh, wow I just added brackets around the variables and it worked

(VARIABLE1,
VARIABLE2,
VARIABLE3,
...
VARIABLE14) = range(14)
like image 133
daxvena Avatar answered Sep 17 '22 14:09

daxvena


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.

like image 45
user225312 Avatar answered Sep 18 '22 14:09

user225312