티스토리 뷰
1. Enumerate Type 사용하기
Enumerate type (열거형)은 파이썬에서 열겨형으로 사용할 수 있으며, 처음 초기화후에는 수정할 수 없다.
auto()함수를 활용하여 순차적으로 값을 할당할 수 있다.
열거형을 리스트에 할당하여 사용할 수 있으며, name, value 속성을 별도로 호출할 수 도 있다.
# 05_00_PythonEnumDataTypes
import enum
class Colors(enum.Enum):
Red = 0 # Red 값에 0을 할당
Green = enum.auto() # Green 값에 1을 할당(Auto는 앞의 값에 +1) 을 자동으로 할당
Yellow = 3 # Yellow 값에 3을 할당
Blue = enum.auto() # Blue 값에 4를 할당
print(Colors) # <enum 'Colors'>
print(type(Colors)) # <class 'enum.EnumMeta'>
print(Colors.Red, Colors.Green, Colors.Yellow, Colors.Blue) # Colors.Red Colors.Green Colors.Yellow Colors.Blue
print(Colors.Red.name, Colors.Green.name, Colors.Yellow.name, Colors.Blue.name) # Red Green Yellow Blue
print(Colors.Red.value, Colors.Green.value, Colors.Yellow.value, Colors.Blue.value) # 0 1 2 4
listColors = [c for c in Colors]
print(listColors) # [<Colors.Red: 0>, <Colors.Green: 1>, <Colors.Yellow: 3>, <Colors.Blue: 4>]
print(listColors[0]) # Colors.Red
print(listColors[0].name) # Red
print(listColors[0].value) # 0
'Python Language' 카테고리의 다른 글
[Python Basic] 07. 기초 연산자 (Basic Operators) (0) | 2020.01.27 |
---|---|
[Python Basic] 06. 셋 데이터 타입(Set Datatype) (0) | 2020.01.22 |
[Python Basic] 04. 날짜와 시간 (Date and Time) (0) | 2019.12.15 |
[Python] 2. Visual Studio Install for Python (1) | 2019.11.12 |
[Python] 1. Visual Studio Install for Python (0) | 2019.10.24 |
댓글