[Python-Dev] PEP 435: initial values must be specified? Yes (original) (raw)

Glenn Linderman v+python at g.nevcal.com
Tue May 7 06:58:39 CEST 2013


On 5/6/2013 7:58 PM, Tim Delaney wrote:

On 7 May 2013 12:29, Ethan Furman <ethan at stoneleaf.us_ _<mailto:ethan at stoneleaf.us>> wrote:

On 05/05/2013 02:55 PM, Tim Delaney wrote:

So long as I can get one of the requirements documented to implement an auto-number syntax I'll be happy enough with stdlib enums I think. class Color(AutoIntEnum): red = ... green = ... blue = ... Will this do? class AutoNumber(Enum): def new(cls): value = len(cls.enuminfo) + 1 obj = object.new(cls) obj.value = value return obj def int(self): return self.value class Color(AutoNumber): red = () green = () blue = () Considering that doesn't actually work with the reference implementation (AutoNumber.new is never called) ... no.

Maybe you should have tried with the latest version of the reference implementation, where Ethan kindly fixed the reference implementation to work better with NamedInt (per my thread "ref impl disc 2") and apparently also with the above class's new...

print(Color.red.value) print(int(Color.red)) ---------- Run Python3 ---------- () Traceback (most recent call last): File "D:\home\repos\mercurial\ref435\ref435.py", line 292, in print(int(Color.red)) TypeError: int returned non-int (type tuple) Plus I would not want to use the empty tuple for the purpose - at least ... implies something ongoing.

Why not? For classes derived from Enum, having new, the value/tuple assigned to the enumeration member becomes the set of parameters to new... so why would you want to provide a parameter? Well, you could, with a minor tweak. If you don't like Ethan's AutoNumber class, you can now write your own, like the following one that I derived from his, but to use your preferred ...

class AutoNumber(Enum): def new(cls, parm): obj = object.new(cls) if parm is ...: value = len(cls.enum_info) + 1 obj._value = value else: obj._value = parm return obj def int(self): return self._value class Color(AutoNumber): red = ... green = ... blue = 7 purple = ...

print ( Color.red, repr( Color.red )) print ( Color.green, repr( Color.green )) print ( Color.blue, repr( Color.blue )) print ( Color.purple, repr( Color.purple ))

Since you want to provide a parameter, I decided in my example AutoNumber class that I would use ... as a flag to use his count, and anything else would be an actual value for the enumeration member. You could do whatever else you like, of course, should you write your own, including using someone's suggested itertools.count() -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130506/088b7f8b/attachment.html>



More information about the Python-Dev mailing list