Python mix-in enumerations as dictionary key: how the type is converted?

Viewed 18659

Here is the code:

from enum import Enum

class EFoo(str, Enum):
    A = 'e1'
    B = 'e2'

print(EFoo.A)

d = {
    EFoo.A : 'eA',
    EFoo.B : 'eB'
}

first_key = list(d.keys())[0]
first_key_type = type(first_key)

print("Keys: " + str(d.keys()))
print("Type of first key: " + str(first_key_type))
print("d[EFoo.A] = '" + d[EFoo.A] + "'")
print("d['e1'] = '" + d['e1'] + "'")

Here is the output (Python 3.5)

EFoo.A  
Keys: dict_keys([<EFoo.A: 'e1'>, <EFoo.B: 'e2'>])  
Type of first key: <enum 'EFoo'>  
d[EFoo.A] = 'eA'  
d['e1'] = 'eA'  

Now the type of the dictionary keys is <enum 'EFoo'>.

So I don't understand why the code d['e1'] = 'eA', which access the dictionary by a string key, works.

Is the string "e1" converted to an "EFoo" instance? Does Python do some reverse lookup to find the correct enum value to convert to?

Moreover, if you remove the str as a parent of the class, so that the class declaration looks like class EFoo(Enum):, the above code snippet does not work anymore What does inheriting from str exactly do in this case?

2 Answers
Related