Re: [Python-Dev] PEP 557: Data Classes

2017年9月11日 15:02:57 -0700

On Sep 10, 2017, at 20:08, Nathaniel Smith <[email protected]> wrote:
> 
> I've sometimes wished that attrs let me control whether it generated equality 
> methods (eq/ne/hash) separately from ordering methods (lt/gt/...). Maybe the 
> cmp= argument should take an enum with options none/equality-only/full?
I have had use cases where I needed equality comparisons but not ordered 
comparisons, so I’m in favor of the option to split them. (atm, I can’t bring 
up a specific case, but it’s not uncommon.)
Given that you only want to support the three states that Nathaniel describes, 
I think an enum makes the most sense, and it certainly would read well. I.e. 
there’s no sense in supporting the ordered comparisons and not equality, so 
that’s not a state that needs to be represented.
I’d make one other suggestion here: please let’s not call the keyword `cmp`. 
That’s reminiscent of Python 2’s `cmp` built-in, which of course doesn’t exist 
in Python 3. Using `cmp` is just an unnecessarily obfuscating abbreviation. 
I’d suggest just `compare` with an enum like so:
enum Compare(enum.Enum):
 none = 1
 unordered = 2
 ordered = 3
One thing I can’t avoid is DRY:
from dataclasses import Compare, dataclass
@dataclass(compare=Compare.unordered)
class Foo:
 # ...
Maybe exposing the enum items in the module namespace?
——dataclasses/__init__.py-----
from enum import Enum
class Compare(Enum):
 none = 1
 unordered = 2
 ordered =3
none = Compare.none
unordered = Compare.unordered
ordered = Compare.ordered
——dataclasses/__init__.py-----
from dataclasses import dataclass, unordered
@dataclass(compare=unordered)
class Foo:
 # ...
Cheers,
-Barry

Attachment: signature.asc
Description: Message signed with OpenPGP

_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to