c enum - how to do this in python?

Anna revanna at mn.rr.com
Tue Mar 4 20:23:10 CET 2003

On Sat, 22 Feb 2003 07:50:16 +0000, Alex Martelli wrote:

> Anna wrote:
>> On Fri, 21 Feb 2003 23:50:12 +0100, Rene Pijlman wrote:
>>> "yelims tluafed .p.l.c eht rof detanimoN"[::-1]
>> Seconded!
>> 'annA'[::-1]
>> --
>> (Gee - I hope that's right... I can't run it in IDLE cuz I get an error
>> that it's not integers... and I'd try from future import but I don't
>> know what it's called...)
> It's only in Python 2.3 (latest release, 2.3a2, the second alpha), not in
> any version of 2.2 (not even with "from __future__ import").

Oh. Never mind...
> Python 2.3 is not yet recommended for production use, being an alpha
> release.  It does have a few neat things such as this one (and sets and
> enumerate and itertools) and is faster than 2.2, so for play/study
> purposes downloading it and installing it might be fun, but be sure to
> keep a 2.2.2 around for "production use"!-)

Sets? So I can write "dogs is dogs and cats is dogs but them turtles
is insects" in Python? Kewl! Hmmm - now I gotta figure out how to put
two different versions of Python on my box without one overwriting the
other... and figure out how to tell it which one to call. Ohboy - new challenges...
> "from __future__ import" is meant to introduce features gradually when
> they BREAK the compatibility of some old code.  When that is the case,
> then for at least one release cycle the old behavior remains the default
> and you have to ask for the new one in this explicit way.  But for
> additions that do not break existing code, there is no need for the
> mechanism, so it's not used.

Oh. Thank you.

> Reversing strings with [::-1] is more or less a divertissement, 

Actually, the more I play with it, the better I like it. It still looks
wierd though... and I still want it to have a NAME. I like names. Names
are a Good Thing. So - what's this mutant smilie called?

> generalized slicing also has serious uses.  Say for example you want to
> return the LAST item in a list that meets some condition.  With
> generalized slicing in Python 2.3 it's easy:
> def lastone(somelist, condition):
>     for item in somelist[::-1]:
>         if condition(item): return item
>     raise ValueError, "No item matches the condition"

Can't you do:

def lastone(somelist, condition):
     for item in somelist.reverse():
         if condition(item): return item
     raise ValueError, "No item matches the condition"

Wouldn't that do the same thing?


More information about the Python-list mailing list