Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Before PEP 435, Python didn't have an equivalent but you could implement your own.</p> <p>Myself, I like keeping it simple (I've seen some horribly complex examples on the net), something like this ...</p> <pre><code>class Animal: DOG = 1 CAT = 2 x = Animal.DOG </code></pre> <hr> <p>In Python 3.4 (<a href="http://www.python.org/dev/peps/pep-0435/" rel="noreferrer">PEP 435</a>), you can make <a href="https://docs.python.org/3/library/enum.html" rel="noreferrer">Enum</a> the base class. This gets you a little bit of extra functionality, described in the PEP. For example, enum members are distinct from integers, and they are composed of a <code>name</code> and a <code>value</code>. </p> <pre><code>class Animal(Enum): DOG = 1 CAT = 2 print(Animal.DOG) # &lt;Animal.DOG: 1&gt; print(Animal.DOG.value) # 1 print(Animal.DOG.name) # "DOG" </code></pre> <hr> <p>If you don't want to type the values, use the following shortcut:</p> <pre><code>class Animal(Enum): DOG, CAT = range(2) </code></pre> <hr> <p><code>Enum</code> implementations <a href="https://docs.python.org/3/library/enum.html#how-are-enums-different" rel="noreferrer">can be converted to lists and are iterable</a>. The order of its members is the declaration order and has nothing to do with their values. For example: </p> <pre><code>class Animal(Enum): DOG = 1 CAT = 2 COW = 0 list(Animal) # [&lt;Animal.DOG: 1&gt;, &lt;Animal.CAT: 2&gt;, &lt;Animal.COW: 0&gt;] [animal.value for animal in Animal] # [1, 2, 0] Animal.CAT in Animal # True </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload