News for hackers. Stuff that matters.
Re:Python's sad, unimaginative Enum by lclarkmichalek on Saturday May 11, 2013 @02:02PM (#5691434)

  > What would you expect a Pythonic enum to look like?
  >
  > class Colour(Enum):
  >     red
  >     green
Hell no. I mean, even Go isn't that implicit, you still have to use the iota there. Anyway, this was considered, and you can see the reasons against it (standard explicit is better than implicit) here: http://www.python.org/dev/peps/pep-0435/#id38

  >  class Colour(Enum):
  >      red = 1
  >      green = 1
  >
  > Still, at least the mistake above would raise an error.  Wouldn't it?  Nope.
  > That's a feature.  If you mess up on the non-optional values then you get
  > "aliases".
Aliases are a feature, like it or not. Having 100% unique enums would not actually be a good thing, seeing as how sometimes libraries support multiple names for the same enum value for backwards compatibility reasons.

And if you do actually want to prevent the duplication problem, you can do this

    class Color(Enum):
        red, green, blue = range(3)
        
        red_alias = red
Or alternatively, if having to specify how many values is too unimaginative for you, something like

    class Color(Enum):
        red, green, blue, *_ = range(2**128)
        
        red_alias = red
will also work.

  > So, you go hunting around in the docs to see if there's any way at all of
  > avoiding the need to assign values manually.  And there is:
  > 
  >   Colour = Enum('Colour', 'red, green')
  >
  > which suffers from the same problems as namedtuples:
  > - you need to repeat the class name (in a string, which your IDE is
  >   unlikely to check)
  > - the parameters are themselves in a string, which your IDE is 
  >   unlikely to parse and provide in auto-complete (they can be separate
  >   strings, in a sequence, but that doesn't really help).
  >
  > Now if two potentially useful library classes are suffering from the same
  > problems than isn't that a BIT OF A HINT to try make things better?  Nope.  It
  > just shows how important it is to not be imaginative.  Or something (crack).
It's true! IDEs don't deal well with meta-programming, and Python isn't Lisp! I don't love the namedtuple syntax particularly either, but what solution would you propose? There doesn't seem to be an easy way around it, afaict.

  > And it gets worse.  What values do you think the above provides?
  >
  > Strings?  That would makes sense (red = 'red'), in that it would display
  > nicely and is going to provide easy to debug values.  So nope.
Well, if we read the PEP (http://www.python.org/dev/peps/pep-0435/#id26):

    >>> class Shake(Enum):
    ...   vanilla = 7
    ...   chocolate = 4
    ...   cookies = 9
    ...   mint = 3
    ...
    >>> for shake in Shake:
    ...   print(shake)
    ...
    Shake.vanilla
    Shake.chocolate
    Shake.cookies
    Shake.mint
We see that printing an enum value gives the string representation, so I don't really think that it not defaulting to strings is that much of a problem.

  > Integers from zero?  I mean that's how Python counts indices and there's "only
  > one way to do it" so that's how Python counts enums, right?  Nope.
I think I almost agree with you here. That said, zero does have a specific meaning in a boolean context, so I can see why they didn't go for it.

  > OK, so bit fields?  That way we can do cool Colour.red | Colour.green and
  > make the C programmers feel at home?  Nope.
Aside from the whole never going to happen argument, I think this kind of shows the problem with doing this implicitly; whatever you do, it's not going to work for some people.

  > Give up?  I'll tell you.  It counts from 1.  Presumably because it's really
  > common to use the "if not enum" idiom.  In someone's crack-addled dreams.
Hey, don't despair so much. You can always read the PEP and realise you can actually provide a dictionary as the second argument. So maybe you'd prefer

    def MyEnum(name, vals):
        return Enum(name, {val: i for i, val in enumerate(vals)})
to have your enums start from 0. I should mention, this information is in the PEP: http://www.python.org/dev/peps/pep-0435/#id35
Re:Python's sad, unimaginative Enum by pjmlp on Saturday May 11, 2013 @06:18PM (#5692313)
> > What would you expect a Pythonic enum to look like? > > > > class Colour(Enum): > > red > > green > > Hell no. I mean, even Go isn't that implicit, you still have to use the iota there.

No, Go just provides plain boring wannabe enums.

Pascal(1972), Modula-2(1978), Ada(1983), and many others:

    type Color = (red, green)

    ord(red) = 1, ord(green) = 2

    len(Color) = 2

    succ(red) = green

    pred(green) = red

    red < green

    type colors = array [Color] of int;
Re:Python's sad, unimaginative Enum by ivanca on Saturday May 11, 2013 @04:40PM (#5692025)
Almost any starting special character would be good enough to make it explicit:

    class Colour(Enum):
        * red
        * green
        * blue
        * none, transparent, empty
Re:Python's sad, unimaginative Enum by ansgri on Sunday May 12, 2013 @04:39AM (#5693630)
Looks like a hybrid of Python and Markdown. And I think such thing deserves to exist!
Re:Python's sad, unimaginative Enum by omaranto on Saturday May 11, 2013 @05:52PM (#5692234)

    class Color(Enum):
        red, green, blue, *_ = range(2**128)
        
        red_alias = red
Does that create a list of size 2^128-3 = 340282366920938463463374607431768211453 and assign it to Color._?
Re:Python's sad, unimaginative Enum by lclarkmichalek on Saturday May 11, 2013 @06:18PM (#5692316)
Ha! Too much Go for me. It does actually, my bad, I expected the variable to be ignored (and the range creation isn't a big deal usually as it's just a generator).
Re:Python's sad, unimaginative Enum by wasabian on Saturday May 11, 2013 @07:17PM (#5692491)
Using range would create the whole list. xrange is the one that acts like a generator :)
Re:Python's sad, unimaginative Enum by andreasvc on Saturday May 11, 2013 @07:28PM (#5692524)
This Enum is slated to be introduced in Python 3.4, where range is (not acts like) a generator.
Re:Python's sad, unimaginative Enum by wasabian on Saturday May 11, 2013 @07:36PM (#5692545)
Ah. I stand corrected :)
Re:Python's sad, unimaginative Enum by omaranto on Saturday May 11, 2013 @08:39PM (#5692675)
But even with a generator, as you would get from range in Python 3, the assignment

    a, *b = range(10)
makes b into a list. (And I think the star syntax in assignments is only Python 3, as far as I remember Python 2 only has star in argument lists of functions.)
Re:Python's sad, unimaginative Enum by __del__ on Sunday May 12, 2013 @05:10AM (#5693682)
You're right, python2 doesn't have the extended unpacking (star) syntax. Furthermore, this:

    >>> a, *b = range(2**128)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: Python int too large to convert to C ssize_t
Ha.
Re:Python's sad, unimaginative Enum by andreasvc on Saturday May 11, 2013 @07:34PM (#5692538)
Yes it does and that is because it actually assigns all of the rest to a variable named '_'. The use of the underscore as a throwaway variable is just a convention and the interpreter is not aware that the result should be ignored.
Re:Python's sad, unimaginative Enum by jspiros on Saturday May 11, 2013 @06:09PM (#5692294)
Looks like it.