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


Groups > comp.lang.python > #45292 > unrolled thread

Re: Python's sad, unimaginative Enum

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2013-05-14 11:24 +0200
Last post2013-05-14 19:40 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#45292 — Re: Python's sad, unimaginative Enum

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2013-05-14 11:24 +0200
SubjectRe: Python's sad, unimaginative Enum
Message-ID<mailman.1666.1368523443.3114.python-list@python.org>
----- 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.

[toc] | [next] | [standalone]


#45320

Fromrusi <rustompmody@gmail.com>
Date2013-05-14 19:40 -0700
Message-ID<64ab830a-f257-4b92-a8f3-6e57981a194f@a10g2000pbr.googlegroups.com>
In reply to#45292
On May 14, 2:24 pm, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
> ----- 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.htmlif 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)


Enums are like names.
And like names are impossible to do right:
http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web