Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45292

Re: Python's sad, unimaginative Enum

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=839f0124b=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; 'lawrence': 0.09; 'solution,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'useful,': 0.14; 'attr': 0.16; 'definition.': 0.16; 'enum': 0.16; 'fits': 0.16; 'subject:skip:u 10': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'do,': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'written': 0.21; 'cc:addr:python.org': 0.22; 'url:moin': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'compare': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; 'url:mailman': 0.30; "skip:' 10": 0.31; 'url:wiki': 0.31; 'proposing': 0.31; 'steven': 0.31; 'class': 0.32; 'know.': 0.32; 'skip:c 30': 0.32; 'quite': 0.32; 'url:python': 0.33; '-----': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'definition': 0.35; '+0200,': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'thank': 0.38; 'little': 0.38; 'does': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'read': 0.60; 'easy': 0.60; 'mentioned': 0.61; "you're": 0.61; 'you.': 0.62; 'information': 0.63; 'received:194': 0.64; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; '100%': 0.77; 'medium.': 0.91; 'novice': 0.91; '2013': 0.98
X-IronPort-AV E=Sophos;i="4.87,669,1363129200"; d="scan'208";a="1458249"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 14 May 2013 11:24:02 +0200 (CEST)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>
In-Reply-To <51919aad$0$29997$c3e8da3$5496439d@news.astraweb.com>
Subject Re: Python's sad, unimaginative Enum
MIME-Version 1.0
X-Mailer Zimbra 7.2.2_GA_2852 (ZimbraWebClient - GC7 (Linux)/7.2.2_GA_2852)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1666.1368523443.3114.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368523444 news.xs4all.nl 15887 [2001:888:2000:d::a6]:35271
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45292

Show key headers only | View raw


----- Original Message -----
> On Mon, 13 May 2013 13:00:36 +0200, Jean-Michel Pichavant wrote:
> 
> > ----- Original Message -----
> >> That's the title of this little beast
> >> http://www.acooke.org/cute/Pythonssad0.html if anybody's
> >> interested.
> >> 
> >> --
> >> If you're using GoogleCrap™ please read this
> >> http://wiki.python.org/moin/GoogleGroupsPython.
> >> 
> >> Mark Lawrence
> >> 
> >> --
> >> http://mail.python.org/mailman/listinfo/python-list
> >> 
> >> 
> > python 2.5
> > 
> > class Enum:
> >   class __metaclass__(type):
> >     def __iter__(self):
> >       for attr in sorted(dir(self)):
> >         if not attr.startswith("__"):
> >           yield getattr(self, attr)
> > 
> > class Colours(Enum):
> >   RED = "red"
> >   GREEN = "green"
> 
> py> class Experience(Enum):
> ...     NOVICE = 'novice'
> ...     GREEN = 'green'
> ...     EXPERIENCED = 'experienced'
> ...     MASTER = 'master'
> ...
> py>
> py> Colours.GREEN == Experience.GREEN
> True
> 
> 
> Oops.
> 
> 
> It's very easy to make something which does a few things that enums
> should do, and call it an Enum. It's much harder to do a lot of
> things
> that enums should do.
> 
> 
> 
> --
> Steven

I was just proposing a solution I've been using and found quite satisfactory. As for the perfect "enumness" of that solution, I don't know. To be honest, I'm not sure I know the exact definition of an enum, and whether or not the C enum fits 100% that definition. It does the job in python. Some people may find it useful, others may just ignore it.
Additionally, the "bug" you mentioned can be written in C as well, casts allow to compare apples and oranges:

(Colours::GREEN == (enum Coulours::Colour)Experiences::GREEN)


JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Python's sad, unimaginative Enum Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-05-14 11:24 +0200
  Re: Python's sad, unimaginative Enum rusi <rustompmody@gmail.com> - 2013-05-14 19:40 -0700

csiph-web