[Python-Dev] PEP 557: Data Classes (original) (raw)
Nathaniel Smith njs at pobox.com
Sun Sep 10 23:08:58 EDT 2017
- Previous message (by thread): [Python-Dev] PEP 557: Data Classes
- Next message (by thread): [Python-Dev] PEP 557: Data Classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Eric,
A few quick comments:
Why do you even have a hash= argument on individual fields? For the whole class, I can imagine you might want to explicitly mark a whole class as unhashable, but it seems like the only thing you can do with the field-level hash= argument is to create a class where the hash and eq take different fields into account, and why would you ever want that?
Though honestly I can see a reasonable argument for removing the class-level hash= option too. And even if you keep it you might want to error on some truly nonsensical options like defining hash without eq. (Also watch out that Python's usual rule about defining eq blocking the inheritance of hash does not kick in if eq is added after the class is created.)
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?
The "why not attrs" section kind of reads like "because it's too popular and useful"?
-n
On Sep 8, 2017 08:44, "Eric V. Smith" <eric at trueblade.com> wrote:
Oops, I forgot the link. It should show up shortly at https://www.python.org/dev/peps/pep-0557/.
Eric.
On 9/8/17 7:57 AM, Eric V. Smith wrote:
I've written a PEP for what might be thought of as "mutable namedtuples with defaults, but not inheriting tuple's behavior" (a mouthful, but it sounded simpler when I first thought of it). It's heavily influenced by the attrs project. It uses PEP 526 type annotations to define fields. From the overview section:
@dataclass class InventoryItem: name: str unitprice: float quantityonhand: int = 0 def totalcost(self) -> float: return self.unitprice * self.quantityonhand Will automatically add these methods: def init(self, name: str, unitprice: float, quantityonhand: int = 0) -> None: self.name = name self.unitprice = unitprice self.quantityonhand = quantityonhand def repr(self): return f'InventoryItem(name={self.name!r},unitprice={self.unitpri ce!r},quantityonhand={self.quantityonhand!r})' def eq(self, other): if other.class is self.class: return (self.name, self.unitprice, self.quantityonhand) == (other.name, other.unitprice, other.quantityonhand) return NotImplemented def ne(self, other): if other.class is self.class: return (self.name, self.unitprice, self.quantityonhand) != (other.name, other.unitprice, other.quantityonhand) return NotImplemented def lt(self, other): if other.class is self.class: return (self.name, self.unitprice, self.quantityonhand) <_ _(other.name, other.unitprice, other.quantityonhand)_ _return NotImplemented_ _def _le_(self, other):_ _if other._class_ is self._class_:_ _return (self.name, self.unitprice, self.quantityonhand) <=_ _(other.name, other.unitprice, other.quantityonhand)_ _return NotImplemented_ _def _gt_(self, other):_ _if other._class_ is self._class_:_ _return (self.name, self.unitprice, self.quantityonhand) > (other.name, other.unitprice, other.quantityonhand) return NotImplemented def ge(self, other): if other.class is self.class: return (self.name, self.unitprice, self.quantityonhand) >= (other.name, other.unitprice, other.quantityonhand) return NotImplemented Data Classes saves you from writing and maintaining these functions. The PEP is largely complete, but could use some filling out in places. Comments welcome! Eric. P.S. I wrote this PEP when I was in my happy place.
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/njs% 40pobox.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20170910/517d166d/attachment-0001.html>
- Previous message (by thread): [Python-Dev] PEP 557: Data Classes
- Next message (by thread): [Python-Dev] PEP 557: Data Classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]