PEP 354 -- "in" operator?

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Feb 27 20:37:50 EST 2006


[fixing References field. Please reply in the original thread so I can
find you easily.]

Roy Smith <roy at panix.com> writes:
> If I have an enum, how can I verify that it's a legal value?  Can I
> do:
>
> Weekdays = enum('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat')
>
> def foo (day):
>    if day not in Weekdays:
>       raise ValueError

The current PEP text doesn't specify; that's an omission on my
part. The answer should be "yes, an enum can be tested for
membership".

I'll add that to the specification.

> Also, do enums have __dict__ slots?

Not specified as having one, no.

> Can I do something like:
>
> day = 'sun'
> print Weekdays.__dict__[day]

You can do the same thing more elegantly by explicitly querying the
attributes of the enumeration::

    >>> day = 'sun'
    >>> print getattr(Weekdays, day)
    sun

-- 
 \            "There was a point to this story, but it has temporarily |
  `\                 escaped the chronicler's mind."  -- Douglas Adams |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list