<div dir="ltr">On 7 May 2013 14:54, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="im">On 05/06/2013 07:58 PM, Tim Delaney wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Considering that doesn't actually work with the reference implementation (AutoNumber.__new__ is never called) ... no.<br>
</blockquote>
<br></div>
Two points:<br>
<br>
1) Did you grab the latest code? That exact implementation passes in the tests.<br></blockquote><div><br></div><div style>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__ ...</div>
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
2) You can write your __new__ however you want -- use ... ! ;)</blockquote><div><br></div><div><div> class AutoNumber(Enum):</div><div> def __new__(cls, value):</div><div> if value is Ellipsis:</div>
<div> try:</div><div> value = cls._auto_number</div><div> except AttributeError:</div><div> value = cls._auto_number = 0</div><div> else:</div>
<div> cls._auto_number = int(value)</div><div><br></div><div> obj = object.__new__(cls)</div><div> obj._value = value</div><div> cls._auto_number += 1</div><div> return obj</div>
<div><br></div><div> def __int__(self):</div><div> return self._value</div><div><br></div><div> class Color(AutoNumber):</div><div> red = ...</div><div> green = 3</div><div> blue = ...</div>
<div><br></div><div><div> print(repr(Color.red))</div><div> print(repr(Color.green))</div><div> print(repr(Color.blue))</div></div></div><div><br></div><div><div>---------- Run Python3 ----------</div><div><Color.red: 0><br>
</div><div><Color.green: 3></div><div><Color.blue: 4></div><div><br></div></div><div style>Unfortunately, if you subclass AutoNumber from IntEnum it breaks.</div><div><br></div><div><div>---------- Run Python3 ----------</div>
<div>Traceback (most recent call last):<br></div><div> File "D:\home\repos\mercurial\ref435\ref435.py", line 346, in <module></div><div> class Color(AutoNumber):</div><div> File "D:\home\repos\mercurial\ref435\ref435.py", line 184, in __new__</div>
<div> enum_item = __new__(enum_class, *args)</div><div>TypeError: int() argument must be a string or a number, not 'ellipsis'</div><div><br></div></div><div style>I would probably also suggest 2 changes:<br></div>
<div style><br></div><div style>1. Set enum_item._name before calling enum_item.__init__.</div><div style><br></div><div style>2. Don't pass any arguments to enum_item.__init__ - the value should be set in enum_item.__new__.</div>
<div><br></div><div style>Tim Delaney </div></div></div></div>