Enum

import enum


#@enum.unique
class Status(enum.Enum):
    ACTIVE = 1
    RENAME_ACTIVE = 1 #I get an error if I have a decorator
    #ACTIVE =2 I get an error
    INACTIVE =2
    RUNNING = 3


print(Status.ACTIVE)
#print(Status.STOPPING)I get an error
print(Status.RENAME_ACTIVE) #ACTIVE returns
print(repr(Status.ACTIVE))
print(Status.ACTIVE.name) #ACTIVE
print(Status.ACTIVE.value) #1

for s in Status:
    print(s)
    print(type(s))

print(Status(1)) #Status.ACTIVE

Execution result:

Status.ACTIVE
Status.ACTIVE
<Status.ACTIVE: 1>
ACTIVE
1
Status.ACTIVE
<enum 'Status'>
Status.INACTIVE
<enum 'Status'>
Status.RUNNING
<enum 'Status'>
Status.ACTIVE
import enum


class Status(enum.IntEnum):
    ACTIVE = 1
    INACTIVE =2
    RUNNING = 3


print(Status.ACTIVE)  # Status.ACTIVE
print(type(Status.ACTIVE))  # <enum 'Status'>
print(Status.ACTIVE == 1)  #It will be replaced with a numerical value only when it is judged as True.
import enum


class Perm(enum.IntFlag):
    R = 4
    W = 2
    X = 1


print(Perm.R | Perm.W)  # Perm.R|W
print(repr(Perm.R | Perm.W | Perm.X))  # <Perm.R|W|X: 7>
RWX = Perm.R | Perm.W | Perm.X
print(Perm.W in RWX)  # True

Recommended Posts

Enum
Python: Synthetic Enum