PEP 354: Enumerations in Python

Paul Rubin http
Mon Feb 27 03:43:45 EST 2006


Steven D'Aprano <steve at REMOVEMEcyber.com.au> writes:
Steven D'Aprano <steve at REMOVEMEcyber.com.au> writes:
> What is an empty enum? 

An empty enum is an enum with no identifiers, just like an empty list
is a list with no elements.

> How and when would you use it?

> The best I can come up with is that an empty enum would be the
> enumerated values you have when you don't actually have any enumerated
> values. This is not to be confused with an empty list: an empty list
> is a well-defined concept. It is just a list (a container) with
> nothing in it. A list of X is like a box containing X, and like boxes,
> an empty list (or set, or dict) is meaningful. An enum of X is just
> the Xs themselves. If there is no X, there is nothing there.

An enum X is a datatype and if the enum is empty then there are no
values for that type.  There's nothing wrong with that.

    def print_members(header, e):  # print header, then members of enum e
      print header
      for m in e:
        print '\t', str(m)

    months_longer_than_february = enum('jan', 'mar', 'apr', ) # etc
    months_shorter_than_february = enum()

    print_members('shorter:', months_shorter_than_february)
    print_members('longer:', months_longer_than_february)

should do the obvious thing.

Hmm, I also see the PEP doesn't specify what's supposed to happen with

  Weekdays = enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
  Solar_system = enum('sun', 'mercury', 'venus', 'earth',)  # etc.
  print Weekdays.sun == Solar_system.sun

so that's another shortcoming with the PEP. 

> But if you have a good usage case for an empty enum, please feel free
> to tell us.

Do you have a good usage case for the number
647574296340241173406516439806634217274336603815968998799147348150763731 ?

If not, maybe we should modify Python so that all arithmetic operations
throw exceptions if they ever encounter that number.  It's unlikely to
ever happen (that number came from os.urandom).  

The issue is that Python operations should work uniformly on all values
that make sense.  Set operations should work on the empty set.  Addition
should work on zero.   Enums with no members are similarly perfectly valid
and should work.  Kernighan and Plauger in "Software Tools" explained it,
"Make sure your code 'does nothing' gracefully".



More information about the Python-list mailing list