Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python enum34 access by name

I am using the Enum backport enum34 with Python 2.7.

According to the documentation it should be possible to access enum members by their name, using item access. That is, the following should work:

from enum import Enum

class Foo(Enum):
    bar = 1
    baz = 2

print(Foo['bar'])

However, when I run the code I get this error in the last line:

TypeError: 'type' object has no attribute '__getitem__'

Am I missing something here or is this functionallity just not implemented in the 2.7 backport?

like image 821
luator Avatar asked Sep 22 '15 14:09

luator


1 Answers

You might be getting a conflict with the Enum module. Try this:

pip uninstall Enum

With both Enum and Enum34 installed, this did not work. After uninstalling Enum, it worked like a charm.

like image 136
CodeLikeBeaker Avatar answered Nov 13 '22 00:11

CodeLikeBeaker