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

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

which is a fabulous syntax, IMHO.

-tkc

[1]
http://www.python.org/dev/peps/pep-3132/

[2]
http://docs.python.org/dev/library/itertools.html#itertools.count

[3]
http://groups.google.com/group/comp.lang.python/browse_thread/thread/63dce474e196adac/d98522d9bedae946#d98522d9bedae946




More information about the Python-list mailing list