Created
December 3, 2020 23:10
-
-
Save jxramos/80a31ccc597709a5f564ea2299693bb9 to your computer and use it in GitHub Desktop.
Cheatsheet for python enum functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import enum | |
class my_enum(enum.Enum) : | |
one = 1 | |
two = 2 | |
three = 3 | |
# Accessors | |
print( "Accessors --------------------------------------------") | |
print( my_enum.one ) | |
print( repr( my_enum.one ) ) | |
print( f"my_enum.one = { my_enum.one }") | |
print( f"my_enum.one.name = { my_enum.one.name }") | |
print( f"my_enum.one.value = { my_enum.one.value }") | |
# Converters | |
print( "\nConverters -----------------------------------------") | |
print( f"my_enum(1)={ my_enum(1) }" ) | |
print( f"my_enum['one']={ my_enum['one'] }" ) | |
# Iterators | |
print( "\nIteration ------------------------------------------") | |
for e in my_enum : | |
print( e ) | |
# Membership | |
print( "\nMembership -----------------------------------------") | |
print( f"my_enum.one in my_enum = {my_enum.one in my_enum }" ) | |
print( f"my_enum.__members__ = {my_enum.__members__ }") | |
print( f"'one' in my_enum.__members__ = {'one' in my_enum.__members__ }") | |
print( f"1 in my_enum.__members__.values() = {1 in my_enum.__members__.values() }" ) | |
print( f"1 in my_enum._value2member_map_ = {1 in my_enum._value2member_map_ }" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment