Ana Sayfa / Python / Python Enum

Python Enum

Creating an enum (Python 2.4 through 3.3)

Enums have been backported from Python 3.4 to Python 2.4 through Python 3.3. You can get this the enum34 backport from PyPI.

pip install enum34
Creation of an enum is identical to how it works in Python 3.4+

from enum import Enum

class Color(Enum):
 red = 1
 green = 2
 blue = 3

print(Color.red) # Color.red 
print(Color(1)) # Color.red 
print(Color['red']) # Color.red

Iteration
Enums are iterable:

class Color(Enum):
 red = 1
 green = 2
 blue = 3

[c for c in Color] # [<Color.red: 1>, <Color.green: 2>, <Color.blue: 3>]

Bunada Göz Atın

Python Date Formatting

Time between two date-times from datetime import datetime a = datetime(2016,10,06,0,0,0) b = datetime(2016,10,01,23,59,59) a-b …

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir