Jaka jest powszechna praktyka enum w Pythonie? [duplikat]

to pytanie ma już odpowiedzi tutaj : Zamknięte 11 lat temu .

Możliwy duplikat:
Jak mogę reprezentować 'enum' w Pythonie?

Jaka jest powszechna praktyka enum w Pythonie? Czyli jak są replikowane w Pythonie?

public enum Materials
{
    Shaded,
    Shiny,
    Transparent,
    Matte
}
 148
Author: Community, 2009-03-31

4 answers

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

>>> print Materials.Matte
3
 374
Author: Van Gale,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-31 20:39:19

Widziałem ten wzór kilka razy:

>>> 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

Możesz również po prostu użyć członków klasy, chociaż będziesz musiał podać własną numerację:

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

>>> Foo.quux
2

Jeśli szukasz czegoś bardziej solidnego (rzadkie wartości, wyjątek specyficzny dla enum itp.), wypróbuj ten przepis .

 20
Author: Ben Blank,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-11 03:15:27

Nie mam pojęcia, dlaczego Enums nie są wspierane natywnie przez Pythona. Najlepszym sposobem na ich emulację jest nadpisanie _ str _ i _ eq_, dzięki czemu można je porównać, A gdy używasz print (), otrzymujesz ciąg znaków zamiast wartości liczbowej.

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

Użycie:

>>> s = enumSeason(enumSeason.Spring)

>>> print(s)

Spring
 10
Author: Spell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-31 20:29:02

Prawdopodobnie przydałaby ci się struktura dziedziczenia, chociaż im bardziej się tym bawiłem, tym bardziej czułem się brudny.

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
 7
Author: Trey Stout,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-31 20:39:07