Greetings,
Eli asked me to put the reference implementation here for review.
It is available at https://bitbucket.org/stoneleaf/aenum in ref435.py and test_ref435.py
Greetings,
Eli asked me to put the reference implementation here for review.
It is available at https://bitbucket.org/stoneleaf/aenum in ref435.py and test_ref435.py
On 04/30/2013 03:34 PM, Ethan Furman wrote:
On 04/30/2013 03:24 PM, Glenn Linderman wrote:
On 4/30/2013 1:12 PM, Ethan Furman wrote:
Greetings,
Eli asked me to put the reference implementation here for review.
It is available at https://bitbucket.org/stoneleaf/aenum in ref435.py and test_ref435.py
Thanks for the code reference.
Tests ran fine here on Python 3.3
If I alter test_ref435.py at the end, as follows, I get an error: nothing matches 'BDFL'
Can someone explain why?
if __name__ == '__main__':
class AnotherName( Name ):
'just uses prior names'
print(AnotherName['BDFL'])
Because Guido said no subclassing.
At this point, if you try to subclass all your getting is the same type. So AnotherName is a string Enumeration.
It wouldn't be hard to check for instances of the Enum in question, and if there are some to raise an error instead. That way:
--> class StrEnum(str, Enum):
... 'str-based enumerations'
--> class Names(StrEnum): # this works, as StrEnum has no instances
... BDFL = 'GvR'
--> class MoreNames(Names): # this fails, as Names has instances
Thoughts?
Latest code available at https://bitbucket.org/stoneleaf/aenum.
--> class Color(Enum):
... red = 1
... green = 2
... blue = 3
Enum items are virtual attributes looked by EnumType's __getattr__. The win here is that
--> Color.red.green.blue
no longer works. ;)
Subclassing an implemented Enum class now raises an error (is there a better word than 'implemented'?)
--> class MoreColor(Color):
... cyan = 4
... magenta = 5
... yellow = 6
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./ref435.py", line 83, in __new__
raise EnumError("cannot subclass an implemented Enum class")
ref435.EnumError: cannot subclass an implemented Enum class
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'd be glad to drop both of those in favor of subclassing: I think the
On 05/01/2013 12:14 PM, Guido van Rossum wrote:
> But we'd probably have to give up something else, e.g. adding methods
> to enums, or any hope that the instance/class/subclass relationships
> make any sense.
emphasis on "class-ness" makes no sense, given the driving usecases for
adopting enums into the stdlib in the first place. IOW, I would vote
that real-world usecases trump hypothetical purity.
If enums had an "as_dict" method that returned an ordered dictionary, you could do:
class MoreColors(Enum):
locals().update(Colors.as_dict())
orange = 4
...
Using a similar API to PEP 422's class initialisation hook, you could even simplify that to:
class MoreColors(Enum, namespace=Colors.as_dict()):
orange = 4
...
Cheers,
Nick.
>
> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Python-Dev mailing list
> Pytho...@python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com