[Python-ideas] Enums

ron3200 ron3200 at gmail.com
Thu Jul 28 09:08:10 CEST 2011


On Wed, 2011-07-27 at 19:21 -0700, Guido van Rossum wrote:


> It's just possible that there's no way to define enums that neither
> introduced a new (class) scope nor requires a lot of redundant typing.
> 
> I wish I could write
> 
>   red = Enum(1)
> 
> and it made the following true:
> 
>   assert red == 1
>   assert isinstance(red, int)  # a subclass
>   assert str(red) == 'red'
> 
> But we'd first need a non-hacky way for Enum() to know that it is
> being assigned to something named 'red'.


I think the least-hacky way is to live with the redundant typing. 

When these objects are passed around, (to/from functions or methods),
they will most likely be rebound to more generic names that don't match
their string repr anyway.

The label is not directly tied to the name the object is bound to,
although it may match at creation time.


What name would you give...
   
        colors = []
        colors.append(Enum(1))
        assert str(colors[0]) == ?



The only other nice alternatives I can think of at the moment requires
either special syntax or a new keyword.
         
         red := 1            # same as...  red = Enum('red', 1)

         enum red, 1      # same as...  red = Enum('red', 1)


I'd still opt for the more explicit Enum('red', 1) expression first
before adding either the syntax or keyword version.  Those can come
later if it turns out enums are hugely popular.



The idea of a new class scope for this hurts my head.  To many what if's
to consider.

Cheers, 
      Ron








   




























-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110728/43031f76/attachment.html>


More information about the Python-ideas mailing list