bug or feature in enum34 py2.7 backport?

Mark Summerfield list at qtrac.plus.com
Wed Nov 26 16:14:00 CET 2014


I only mentioned it since I noticed it. I actually use Python 3 so it isn't a problem for me, but sometimes I have to teach Python 2.7 and I wanted to cover enum because it is so much nicer and easier to debug than FOO = 1 etc.

However, the problem is that enum's function API doesn't play nicely with unicode literals. One solution is to use the class API instead:


from __future__ import print_function
from __future__ import unicode_literals
import enum
print(enum.version)
class A(enum.Enum):
   b = 1
   c = 2
print(A.b, A.c) 


On Wednesday, November 26, 2014 2:41:16 PM UTC, Chris Angelico wrote:
> On Thu, Nov 27, 2014 at 1:05 AM,  <fastmail.us> wrote:
> > On Wed, Nov 26, 2014, at 06:29, Mark Summerfield wrote:
> >> TypeError: type() argument 1 must be string, not unicode
> >
> > If this is a bug, maybe it is one in type() itself - I get the same
> > error with type('X', (object,), dict(a=1))
> 
> I'd say that's a limitation, not a bug. A lot of stuff in Python 2
> depends on identifiers being ASCII-only byte strings, including -
> apparently - parts of the core code. Suggestion: Switch to Python 3,
> or if you can't do that, pass your identifiers through str(). When you
> do eventually switch to Py3, that won't do anything, but in Py2, it'll
> force them to be byte strings. As long as they are actually
> ASCII-only, that'll solve your problem.
> 
> ChrisA




More information about the Python-list mailing list