Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 380
robertspierre Avatar asked Aug 02 '17 11:08

robertspierre


1 Answers

EFoo inherits from str, so it is a str -- that is how inheritance works. Because EFoo inherits from str all its members are strs as well as Efoos, so standard str operations work:

>>> isinstance(EFoo.A, str)
True
>>> EFoo.A.upper()
E1
>>> EFoo.A + EFoo.B
e1e2

Inheriting from str (or any other base type) does remove some of the safeties built in to Enum, so you should only do it if necessary (such as replacing existing constants of that type already in your code).

like image 140
Ethan Furman Avatar answered Oct 04 '22 00:10

Ethan Furman