[Python-Dev] PEP 435: initial values must be specified? Yes (original) (raw)
Tim Delaney timothy.c.delaney at gmail.com
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 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:
Set enum_item._name before calling enum_item.init.
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 ]