ANN: enum 0.4 - Enumerations in Python

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Dec 20 00:12:40 EST 2005


Howdy all,

I've uploaded enum 0.4 to the Cheeseshop.

    <URL:http://cheeseshop.python.org/pypi/enum/>

Main changes:

Comparing values from an enumeration against a value not from the same
enumeration will now succeed (previous versions raised an exception)::

    >>> from enum import Enum
    >>> Weekdays = Enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
    >>> pizza_night = Weekdays.fri
    >>> pizza_night == Weekdays.fri
    True
    >>> pizza_night == Weekdays.tue
    False
    >>> pizza_night == "fri"
    False
    >>> pizza_night == 5
    False

This is done by using the ``NotImplemented`` return value for the
comparison against values that are not from the same enumeration. The
Python interpreter will then attempt other fallbacks instead of
failing.

This allows the values from an enumeration to be sequenced or compared
among heterogeneous values, such as a list::

    >>> things = [23, Weekdays.tue, "spam"]
    >>> things.sort()
    >>> Weekdays.tue in things
    True
    >>> Weekdays.fri in things
    False


Package description:

"""Robust enumerated type support in Python

This package provides a module for robust enumerations in Python.

An enumeration object is created with a sequence of string arguments
to the Enum() constructor::

    >>> from enum import Enum
    >>> Colours = Enum('red', 'blue', 'green')
    >>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')

The return value is an immutable sequence object with a value for each
of the string arguments. Each value is also available as an attribute
named from the corresponding string argument::

    >>> pizza_night = Weekdays[4]
    >>> shirt_colour = Colours.green

The values are constants that can be compared only with values from
the same enumeration; comparison with other values will invoke
Python's fallback comparisons.

    >>> pizza_night == Weekdays.fri
    True
    >>> shirt_colour > Colours.red
    True
    >>> shirt_colour == "green"
    False

Each value from an enumeration exports its sequence index
as an integer, and can be coerced to a simple string matching the
original arguments used to create the enumeration::

    >>> str(pizza_night)
    'fri'
    >>> shirt_colour.index
    2
"""

-- 
 \           "It ain't so much the things we don't know that get us in |
  `\      trouble. It's the things we know that ain't so."  -- Artemus |
_o__)                                  Ward (1834-67), U.S. journalist |
Ben Finney



More information about the Python-list mailing list