Message189854
| Author |
ncoghlan |
| Recipients |
ncoghlan |
| Date |
2013年05月23日.08:56:17 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1369299378.37.0.789518154232.issue18042@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
Another attempt at tackling the "but I want to ensure my enum values are unique" problem that PEP 435 deliberately chose not to handle. My previous suggestion (in issue 17959) was rightly rejected due to the other problems it caused, but this idea is much cleaner and simpler.
All we would need to do is provide the following class decorator in the enum module:
def unique(new_enum):
for name, member in new_enum.__members__.items():
if name != member.name:
msg = "Alias {!r} for {!r} not permitted in unique Enum"
raise TypeError(msg.format(name, member))
return new_enum
Used as so:
>>> @enum.unique
... class MyEnum(enum.Enum):
... a = 1
... b = 2
... c = 1
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 6, in unique
TypeError: Alias 'c' for <MyEnum.a: 1> not permitted in unique Enum |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2013年05月23日 08:56:18 | ncoghlan | set | recipients:
+ ncoghlan |
| 2013年05月23日 08:56:18 | ncoghlan | set | messageid: <1369299378.37.0.789518154232.issue18042@psf.upfronthosting.co.za> |
| 2013年05月23日 08:56:18 | ncoghlan | link | issue18042 messages |
| 2013年05月23日 08:56:17 | ncoghlan | create |
|