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

15,189

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).

Share:
15,189

Related videos on Youtube

robertspierre
Author by

robertspierre

Updated on June 26, 2022

Comments

  • robertspierre
    robertspierre almost 2 years

    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?

  • haridsv
    haridsv over 2 years
    It seems like str enums are a nice step up for global str constants.