Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by gbin on Friday May 10, 2013 @11:28AM
(#5686707)
I prefer the Michael Ford proposed syntax and would gladly port the implementation if possible.
Having to specify either an ordinal number like in BASIC of 1980 or have to specify symbols as strings looks dodgy to me.
[ Parent
| on HN ]
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by Tobu on Friday May 10, 2013 @11:56AM
(#5686893)
Since Enums are mostly unmagical (apart from the member wrapping), you can use a compact syntax:
class Colors(Enum):
red, green, blue = range(3)
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by iso8859-1 on Friday May 10, 2013 @07:00PM
(#5689090)
will
red, green, blue = itertools.count()
work too?
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by Tobu on Friday May 10, 2013 @07:02PM
(#5689099)
If it works in a Python shell, it will work for an enum (this doesn't).
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by BoppreH on Friday May 10, 2013 @02:08PM
(#5687749)
The PEP mentions these explicit values allow the creation of aliases in a very natural way.
class Keys(Enum):
space = 1
esc = 2
enter = 3
return = 3 # Alias to "enter".
Also, less magic.
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by IgorPartola on Friday May 10, 2013 @05:26PM
(#5688782)
return = 3 shouldn't work.
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by mbreese on Friday May 10, 2013 @06:11PM
(#5688962)
you know what they meant...
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by cpeterso on Friday May 10, 2013 @01:07PM
(#5687374)
Michael Ford's proposed syntax looks much nicer. I don't understand why the enum values have to be assigned when they are first looked up. Why couldn't the commas in the definition signal that the enum members should have automatic values assigned at the definition?
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by morphyn on Friday May 10, 2013 @02:03PM
(#5687721)
Doesn't the 'functional API' make up for it ? `Animal = Enum('Animal', ['ant', 'bee', 'cat', 'dog'])`
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by bsimpson on Friday May 10, 2013 @02:30PM
(#5687861)
The functional API could stand to be improved a bit:
>>> Animal = Enum(['ant, 'bee', 'cat', 'dog'])
>>> Animal.ANT
'ant'
I'm not convinced it's useful to pass the class name into the functional API, but I would see value in having an easy API to create enumerated strings. It's easier to see what's going on in other environments (e.g. in a datastore, or in JavaScript) if you pass around string identifiers instead of ints.Here's the class I've been using to do that:
class Constants(object):
def __init__(self, names, enum = False):
self._names = frozenset(names)
if enum:
for (i, name) in enumerate(self._names):
setattr(self, name.upper(), i)
else:
for name in self._names:
setattr(self, name.upper(), name.lower())
self.frozen = True
def keys(self):
return [name.upper() for name in self._names]
def values(self):
return [getattr(self, name.upper()) for name in self._names]
def __setattr__(self, key, value):
if getattr(self, 'frozen', False):
raise Exception("Constants cannot be modified after instantiation")
else:
object.__setattr__(self, key, value)
Example: >>> ImportMethod = Constants(
>>> [
>>> 'email',
>>> 'manual_upload',
>>> 'api',
>>> ]
>>> )
>>>
>>> print(ImportMethod.API)
'api'
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by dietrichepp on Friday May 10, 2013 @03:56PM
(#5688333)
There is a technical reason why the class name is passed into the API, and it's the same for other functions which create types (collections.namedtuple(), and type(), for example). You want to be able to print out the class name when you print an enum, but there is no other reliable way to figure out the class name. In Python, a class is just another value, like functions and integers and its name is nothing special. When you define a class normally, the name and dictionary get passed to the type() function.
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by bsimpson on Friday May 10, 2013 @06:12PM
(#5688966)
I understand that, but I'm not sure it matter. Is it worth the ugliness in the API to print out the class name when you run print? Is 'api' that much worse than "ImportMethod.API"?
Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by dietrichepp on Saturday May 11, 2013 @01:44AM
(#5689808)
That's not all it's there for. Python programmers expect to be able to do things like print(obj.__class__.__name__). You're definitely free to write your own alternative Enum, however, since it's just a library function.