2009-03-31 9 views
114

संभव डुप्लिकेट:
How can I represent an 'enum' in Python?पायथन में enums के लिए आम अभ्यास क्या है?

अजगर में enums के लिए आम बात क्या है? अर्थात। वे पायथन में कैसे दोहराए जाते हैं?

public enum Materials 
{ 
    Shaded, 
    Shiny, 
    Transparent, 
    Matte 
} 
+11

अब पायथन 3.4 में मानक एनम प्रकार है। इस पोस्ट को पढ़ें: http://stackoverflow.com/questions/16653129/future-compatible-enums-in-2-7 – Javier

+2

एनम प्रकार के लिए पीईपी है जो अब पाइथन में है: https: //www.python। संगठन/देव/पेप्स/पेप-0435/ – shuttle87

उत्तर

329
class Materials: 
    Shaded, Shiny, Transparent, Matte = range(4) 

>>> print Materials.Matte 
3 
+14

मैंने इसे पहले नहीं देखा है, अच्छी चीजें। –

+0

अच्छी टिप, धन्यवाद। –

+0

एकमात्र समस्या यह है कि मुझे पहले आइटम की आवश्यकता है 1. क्या यह आपकी विधि से करना संभव है? –

17

मैं इस पैटर्न देखा है कई बार:

>>> class Enumeration(object): 
     def __init__(self, names): # or *names, with no .split() 
      for number, name in enumerate(names.split()): 
       setattr(self, name, number) 

>>> foo = Enumeration("bar baz quux") 
>>> foo.quux 
2 

आप, साथ ही वर्ग के सदस्यों का उपयोग कर सकते हैं, हालांकि आप अपने खुद के नंबर की आपूर्ति करना होगा:

>>> class Foo(object): 
     bar = 0 
     baz = 1 
     quux = 2 

>>> Foo.quux 
2 

हैं आप कुछ और मजबूत (स्पैस वैल्यू, एनम-विशिष्ट अपवाद इत्यादि) की तलाश में हैं, try this recipe

8

मुझे नहीं पता कि क्यों पाइमॉन द्वारा एनम्स का समर्थन नहीं किया जाता है। मुझे अनुकरण करने के लिए सबसे अच्छा तरीका है _ str _ और _ eq _ को ओवरराइड करके, आप उनकी तुलना कर सकते हैं और जब आप प्रिंट() का उपयोग करते हैं तो आपको संख्यात्मक मान के बजाय स्ट्रिंग मिलती है।

class enumSeason(): 
    Spring = 0 
    Summer = 1 
    Fall = 2 
    Winter = 3 
    def __init__(self, Type): 
     self.value = Type 
    def __str__(self): 
     if self.value == enumSeason.Spring: 
      return 'Spring' 
     if self.value == enumSeason.Summer: 
      return 'Summer' 
     if self.value == enumSeason.Fall: 
      return 'Fall' 
     if self.value == enumSeason.Winter: 
      return 'Winter' 
    def __eq__(self,y): 
     return self.value==y.value 

उपयोग:

>>> s = enumSeason(enumSeason.Spring) 

>>> print(s) 

Spring 
+4

पीईपी 354 में अस्वीकृति नोटिस है। देखें http://www.python.org/dev/peps/pep-0354/#rejection-notice –

+4

कक्षा कक्षा {"वसंत": 0, "ग्रीष्मकालीन": 1, ...} होना जल्दबाजी होगी और प्रविष्टियों के माध्यम से पुन: प्रयास करने के लिए __init__ का उपयोग करें और गुण सेट करें, क्योंकि तब __str__ प्रत्येक मामले के लिए हाथ से कोडित होने के बजाय मूल्य को देख सकता है। –

+1

इसे देखें: https://www.python.org/dev/peps/pep-0435/ – shuttle87

6

आप शायद एक विरासत संरचना इस्तेमाल कर सकते हैं, हालांकि अधिक मैं के साथ इस गंदी मुझे लगा निभाई।

class AnimalEnum: 
    @classmethod 
    def verify(cls, other): 
    return issubclass(other.__class__, cls) 


class Dog(AnimalEnum): 
    pass 

def do_something(thing_that_should_be_an_enum): 
    if not AnimalEnum.verify(thing_that_should_be_an_enum): 
    raise OhGodWhy 
संबंधित मुद्दे