On 12/12/20 7:52 PM, Steven D'Aprano wrote:
On Sat, Dec 12, 2020 at 07:01:55PM -0800, Ethan Furman wrote:

That's invalid.  Duplicates allowed means:

```
class K( NamedValue):
      A = 1
      B = 1
```
B is not an alias for A.  Presumably one has the same number with different
meaning.  If that were an Enum:

```
K.B
K.A

Ah! Talking about Boy Looks, I had never noticed that behaviour before.
(But then I don't regularly use duplicate Enum values.)

What is the reason for that behaviour in Enums?

Different names for the same thing, the second names (and third, and ...) are aliases for the first. For example, if you're me and constantly forget to drop the 's', you might have

class Border(Enum):
    LINE = 'line'
    LINES = 'line'

and then, no matter which I type, I get the right answer. Silly example, but the point is that Enum members are singletons, and there should only be one canonical member to represent a single semantic value -- hence the recommendation to compare Enums using `is`.

NamedValues, on the other hand, don't have the concept of set membership and 
one canonical name for a single value.

>> [...]

Class MyEnum(Enum):
     ONE = 1
     TWO = 2

MyEnum.ONE + 3
# TypeError

Class MyValue(NamedValue):
     ONE = 1
     TWO = 2

MyValue.TWO + 3
5

Isn't this the solution to that?

class MyValue(int, Enum):
...     ONE = 1
...     TWO = 2
...
MyValue.TWO + 3
5

It certainly can be abused for that, but the intended purpose of IntEnum is not to support math operations, but rather to interoperate with existing APIs that are using ints as magic numbers.

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2BTEWH4RFOWIWY7QICGH2ZQSFNG5JPTM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to