On 4/30/2013 4:49 PM, Ethan Furman wrote:
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.

Indeed, I heard him.  But what I heard was that subclasses shouldn't be allowed to define new enumeration values, and that was the point of all his justification and the justifications in the Stack Overflow discussion he linked to. I don't want to disagree, or argue that point, there are reasons for it, although some have raised counter-arguments to it.  This is not intended to be a counter-argument to the point that there should be no new enumeration values created in subclasses.


At this point, if you try to subclass all your getting is the same type.  So AnotherName is a string Enumeration.

So if I get the same type, it'd be kind of nice if it worked the same too... even if the instances are of type Name, it seems that one should be able to look them up, the same as one can look them up using Name.

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?

So to me, it would seem quite reasonable to allow only one class in the hierarchy to define instances.  If no instances have been defined before, then defining an enumeration instance should occur for each attribute for which it is appropriate.  But if a superclass has defined enumeration instances, then things defined in subclasses should not be taken as enumeration instances... and the choice should be between an error, and simply allowing it to be defined as a normal class attribute of the subclass.