Python's sad, unimaginative Enum

Discussion in 'Python' started by Mark Lawrence, May 13, 2013.

  1. Mark Lawrence, May 13, 2013
    #1
    1. Advertising

  2. On Mon, 13 May 2013 10:24:40 +0100, Mark Lawrence wrote:

    > That's the title of this little beast
    > http://www.acooke.org/cute/Pythonssad0.html if anybody's interested.




    Well, that's one way of looking at it. And I can't exactly *disagree*.

    But... but...

    In many ways, it's a dull language, borrowing solid old concepts
    from many other languages & styles: boring syntax, unsurprising
    semantics, few automatic coercions, etc etc. But that's one of
    the things I like about it. -- Tim Peters, 16 Sep 93


    Being "sad and unimaginative" is a *good thing* when it comes to syntax.
    Which would you rather read?

    Python:

    try:
    int(astring)
    except ValueError:
    print(False)



    APL:

    ⊃⎕VFI{wâ†âµâ‹„((w='-')/w)â†'¯'â‹„w}


    [pedant: okay, so the two code snippets are not *exactly* equivalent. So
    sue me.]


    Let's look at his major criticisms:

    1) values aren't automatically generated.

    True. So what? That is the *least* important part of enums. The most
    important things about enums are:

    - they aren't strings, but look like symbolic values;

    - or they are ints (for compatibility with C libraries) that look like
    symbolic values.

    Probably half the use-cases for enums are for compatibility with C
    libraries, where you *need* to specify the value. There's no point you
    defining an enum SOCK_RAW, having Python decide to set it to 7, when the
    rest of the world agrees it should have the value 3.


    2) The enum implementation allows duplicates.

    Yes, this *is* a feature. In the real world, enums are not unique. There
    are aliases (maybe you want FAMILY_NAME and SURNAME to be the same enum),
    and misspelled enums need to be corrected:


    class Insects(Enum):
    bee = 2
    ant = 3
    wasp = 4 # Preferred spelling.
    wsap = 4 # Oops, keep this for backward compatibility!


    I'm sorry for all those who can't avoid duplicating values, but really,
    Python doesn't protect them from other silly typos, why are Enums so
    special that they need to be treated differently?


    3) the functional API for creating auto-numbered Enums "suffers from the
    same problems" as namedtuples:

    Steven D'Aprano, May 13, 2013
    #2
    1. Advertising

  3. On Mon, May 13, 2013 at 8:17 PM, Steven D'Aprano
    <> wrote:
    > Let's look at his major criticisms:
    >
    > 1) values aren't automatically generated.
    >
    > True. So what? That is the *least* important part of enums.


    I stopped following the -ideas threads about enums, but IIRC
    autogeneration of values was in quite a few of the specs early on. So
    you can probably find the arguments against it in the list archives.

    FWIW, though, I do like C's autogeneration of enum values - but use it
    in only a small proportion of my enums. It's not a terrible loss, but
    it is a loss.

    ChrisA
    Chris Angelico, May 13, 2013
    #3
  4. Chris Angelicoæ–¼ 2013å¹´5月14日星期二UTC+8上åˆ1時36分34秒寫é“:
    > On Mon, May 13, 2013 at 8:17 PM, Steven D'Aprano
    >
    > <> wrote:
    >
    > > Let's look at his major criticisms:

    >
    > >

    >
    > > 1) values aren't automatically generated.

    >
    > >

    >
    > > True. So what? That is the *least* important part of enums.

    >
    >
    >
    > I stopped following the -ideas threads about enums, but IIRC
    >
    > autogeneration of values was in quite a few of the specs early on. So
    >
    > you can probably find the arguments against it in the list archives.
    >
    >
    >
    > FWIW, though, I do like C's autogeneration of enum values - but use it
    >
    > in only a small proportion of my enums. It's not a terrible loss, but
    >
    > it is a loss.
    >
    >
    >
    > ChrisA


    Because a hash table can replace the enums in other languages,
    it is more pythonic to use a dictionary built first
    to replace the enums.

    I think it is the same style of programming in perl and ruby.
    88888 Dihedral, May 15, 2013
    #4
    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. 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
  4. 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
  5. rusi
    Replies:
    0
    Views:
    100
Loading...

Share This Page