Sunday 12 September 2021

Python: __members__: Get all enum members including aliases

‘__members__’ attribute is used to get all the enum members including aliases.

 

membersAttribute.py

from enum import Enum

class Test(Enum):        
    A = 1
    B = 1
    C = 23

print('list(Test) -> ', list(Test))
print('Test.__members__ -> ' ,Test.__members__)

print('\nIterating over Test.__members__')
for member in Test.__members__.items():
    # member represent a tuple
    print(member[0], '->', member[1])

 

Output

list(Test) ->  [<Test.A: 1>, <Test.C: 23>]
Test.__members__ ->  {'A': <Test.A: 1>, 'B': <Test.A: 1>, 'C': <Test.C: 23>}

Iterating over Test.__members__
A -> Test.A
B -> Test.A
C -> Test.C

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment