[Python-ideas] constant/enum type in stdlib

Antoine Pitrou solipsis at pitrou.net
Wed Jan 30 17:26:27 CET 2013


Le Wed, 30 Jan 2013 15:22:06 +0000,
Michael Foord <fuzzyman at gmail.com> a
écrit :
> On 30 January 2013 07:26, Antoine Pitrou
> <solipsis at pitrou.net> wrote:
> 
> > On Wed, 30 Jan 2013 17:58:37 +1300
> > Greg Ewing <greg.ewing at canterbury.ac.nz>
> > wrote:
> > > Guido van Rossum wrote:
> > >
> > > > class color(enum):
> > > >   RED = value()
> > > >   WHITE = value()
> > > >   BLUE = value()
> > >
> > > We could do somewhat better than that:
> > >
> > >     class Color(Enum):
> > >        RED, WHITE, BLUE = range(3)
> >
> 
> 
> 
> With a Python 3 metaclass that provides default values for *looked up*
> entries you could have this:
> 
> class Color(Enum):
>     RED, WHITE, BLUE

This relies on tuple evaluation order, and would also evaluate any
other symbol looked up from inside the class body (which means I
cannot add anything else than enum symbols to the class).

In other words, I'm afraid it would be somewhat fragile ;)

Regards

Antoine.





More information about the Python-ideas mailing list