enums and PEP 3132

Tim Chase python.list at tim.thechases.com
Wed Mar 12 20:13:24 CET 2008

>> Currently I'm just putting this at the top of the file:
>>     py=1
>>     funcpre=2
>>     funcpost=3
>>     ...
> That can be done more compactly with
>      py, funcpre, funcpost = range(3)

I've harbored a hope that a combination of PEP 3132[1] ("Extended 
Iterable unpacking") and itertools.count()[2] would be available 
for doing something like this:

   py, funcpre, funcpost, *unexhausted_iterator = count()

which would theoretically allow me to just add new enum names to 
the LHS without having to update constant passed to range() on 
the RHS.

Unfortunately, it looks like this wasn't a desirable behavior 
because the PEP describes the "*unexhausted_iterator" notation 
unpacking as a list, not as an iterable.

My desired syntax would work well for bit-mask fields as well:

   def bit_iter(i=0):
     assert i >= 0
     while True:
       yield 1 << i
       i += 1
   read, write, execute, *_ = bit_iter()

and I'm sure there are other use-cases I've not yet considered.

Diez Roggisch hacked together a disturbingly (in the "that hurts 
my brain to sniff the stack" way) beautiful/functional 
decorator[3] that does something like this in Python2.4+ and can 
just be used something like

   def just_enough(): return itertools.count()
   read, write, execute = just_enough()

which is a fabulous syntax, IMHO.





More information about the Python-list mailing list