[Python-Dev] PEP 435: initial values must be specified? Yes
Tim Delaney
timothy.c.delaney at gmail.com
Tue May 7 07:14:25 CEST 2013
More information about the Python-Dev mailing list
Tue May 7 07:14:25 CEST 2013
- Previous message: [Python-Dev] PEP 435: initial values must be specified? Yes
- Next message: [Python-Dev] PEP 435: initial values must be specified? Yes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 7 May 2013 14:54, Ethan Furman <ethan at stoneleaf.us> wrote: > On 05/06/2013 07:58 PM, Tim Delaney wrote: > >> >> Considering that doesn't actually work with the reference implementation >> (AutoNumber.__new__ is never called) ... no. >> > > Two points: > > 1) Did you grab the latest code? That exact implementation passes in > the tests. > D'oh! I had my default path being my forked repo ... so didn't see the changes. BTW I can't see how that exact implementation passes ... not enough parameters declared in AutoNumber.__new__ ... > 2) You can write your __new__ however you want -- use ... ! ;) class AutoNumber(Enum): def __new__(cls, value): if value is Ellipsis: try: value = cls._auto_number except AttributeError: value = cls._auto_number = 0 else: cls._auto_number = int(value) obj = object.__new__(cls) obj._value = value cls._auto_number += 1 return obj def __int__(self): return self._value class Color(AutoNumber): red = ... green = 3 blue = ... print(repr(Color.red)) print(repr(Color.green)) print(repr(Color.blue)) ---------- Run Python3 ---------- <Color.red: 0> <Color.green: 3> <Color.blue: 4> Unfortunately, if you subclass AutoNumber from IntEnum it breaks. ---------- Run Python3 ---------- Traceback (most recent call last): File "D:\home\repos\mercurial\ref435\ref435.py", line 346, in <module> class Color(AutoNumber): File "D:\home\repos\mercurial\ref435\ref435.py", line 184, in __new__ enum_item = __new__(enum_class, *args) TypeError: int() argument must be a string or a number, not 'ellipsis' I would probably also suggest 2 changes: 1. Set enum_item._name before calling enum_item.__init__. 2. Don't pass any arguments to enum_item.__init__ - the value should be set in enum_item.__new__. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20130507/94a42360/attachment.html>
- Previous message: [Python-Dev] PEP 435: initial values must be specified? Yes
- Next message: [Python-Dev] PEP 435: initial values must be specified? Yes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list