Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to live with both enum and enum34?

Tags:

python

enums

I noticed a different behaviour with enum on Python.

I was first surprised to notice the output for this was an int:

>>>import enum
>>>class Color(enum.Enum):
       red = 1

>>>Color.red
1

Then I realized I had enum installed instead of enum34:

$ sudo apt-get install python-enum34

And now, the result is different:

>>>Color.red
<Color.red: 1>

My current application is accepting enum types where I get the value of the enum with value.value. Of course this will raise an exception if the wrong enum is installed.

How can I deal with this issue?

like image 849
nowox Avatar asked May 02 '16 20:05

nowox


1 Answers

As a guess, it looks like you had the enum package that existed before the 3.4 Enum came in to being. enum34 is so named because that previous package already existed.

Both enum and enum34 install to the same location, so making them co-exist is not easy -- plus it would make your code difficult to distribute as one of the enums would be in a non-standard location.

One possibility is to use virtual envs -- then you can install whichever enum is needed for the application in the venv.

like image 87
Ethan Furman Avatar answered Oct 15 '22 19:10

Ethan Furman