Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ Enum ในคลาส Python

Python มีโมดูล enum สำหรับการใช้ Enum ในคลาส

import enum

class Car(enum.Enum):
    suzuki = 1
    Hyundai = 2
    Dezire = 3

print("All the enum values are:")
for c in Car:
    print(c)

Enum ช่วยให้กำหนดค่าคงที่กลุ่มได้อย่างชัดเจน และสามารถวนลูปเข้าถึงค่าแต่ละค่าได้