Message188981
Creating this as a separate issue so as not to delay incorporation of the accepted PEP.
One legitimate criticism of the accepted PEP 435 is that the combination of requiring explicit assignment of values *and* allowing aliasing by default is that aliases may be created inadvertently. I believe we can actually do better than the initial implementation by making the following work:
>>> class Shape(Enum):
... square = 2
... diamond = 1
... circle = 3
... alias_for_square = square
...
>>> Shape.square
<Shape.square: 2>
>>> Shape.alias_for_square
<Shape.square: 2>
>>> Shape(2)
<Shape.square: 2>
While *disallowing* the following:
>>> class Shape(Enum):
... square = 2
... diamond = 1
... circle = 3
... alias_for_square = 2
...
How, do you ask? By wrapping non-descriptors on assignment in a placeholder type, and keeping track of which values we have already seen. If a new attribute is mapped to a placeholder, then that's fine, we accept it as an explicit declaration of an alias. However, if it's mapped directly to a repeat value, then that would be disallowed (as it was in earlier versions of the PEP). |
|
Date |
User |
Action |
Args |
2013-05-12 06:51:37 | ncoghlan | set | recipients:
+ ncoghlan |
2013-05-12 06:51:37 | ncoghlan | set | messageid: <1368341497.6.0.896861896968.issue17959@psf.upfronthosting.co.za> |
2013-05-12 06:51:37 | ncoghlan | link | issue17959 messages |
2013-05-12 06:51:37 | ncoghlan | create | |
|