[New-bugs-announce] [issue17959] Alternate approach to aliasing for PEP 435
Nick Coghlan
report at bugs.python.org
Sun May 12 08:51:37 CEST 2013
New submission from Nick Coghlan:
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).
----------
messages: 188981
nosy: ncoghlan
priority: normal
severity: normal
stage: needs patch
status: open
title: Alternate approach to aliasing for PEP 435
type: enhancement
versions: Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17959>
_______________________________________
More information about the New-bugs-announce
mailing list