Re: Python's sad, unimaginative Enum

Discussion in 'Python' started by rusi, May 15, 2013.

  1. rusi

    rusi Guest

    On May 14, 2:24 pm, Jean-Michel Pichavant <>
    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/
    rusi, May 15, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wipkip

    A very sad ? about frames

    Wipkip, Oct 15, 2003, in forum: HTML
    Replies:
    8
    Views:
    340
  2. Zac Burns

    Why is python so sad?

    Zac Burns, Oct 14, 2009, in forum: Python
    Replies:
    0
    Views:
    249
    Zac Burns
    Oct 14, 2009
  3. Mark Lawrence

    Python's sad, unimaginative Enum

    Mark Lawrence, May 13, 2013, in forum: Python
    Replies:
    3
    Views:
    112
    88888 Dihedral
    May 15, 2013
  4. Steven D'Aprano

    Re: Python's sad, unimaginative Enum

    Steven D'Aprano, May 14, 2013, in forum: Python
    Replies:
    0
    Views:
    73
    Steven D'Aprano
    May 14, 2013
  5. Fábio Santos

    Re: Python's sad, unimaginative Enum

    Fábio Santos, May 14, 2013, in forum: Python
    Replies:
    0
    Views:
    101
    Fábio Santos
    May 14, 2013
Loading...

Share This Page