[Python-Dev] PEP 557: Data Classes

Stéfane Fermigier sf at fermigier.com
Sat Sep 9 06:02:45 EDT 2017


Hi, first post here.

My two cents:

Here's a list of "prior arts" that I have collected over the years, besides
attrs, that address similar needs (and often, much more):

- https://github.com/bluedynamics/plumber
- https://github.com/ionelmc/python-fields
- https://github.com/frasertweedale/elk
- https://github.com/kuujo/yuppy

Regarding the name, 'dataclass', I agree that it can be a bit misleading
(my first idea of a "dataclass" would be a class with only data and no
behaviour, e.g. a 'struct', a 'record', a DTO, 'anemic' class, etc.).

Scala has 'case classes' with some similarities (
https://docs.scala-lang.org/tour/case-classes.html).

Regards,

  S.


On Fri, Sep 8, 2017 at 4:57 PM, Eric V. Smith <eric at trueblade.com> 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
>     unit_price: float
>     quantity_on_hand: int = 0
>
>     def total_cost(self) -> float:
>         return self.unit_price * self.quantity_on_hand
>
> Will automatically add these methods:
>
>   def __init__(self, name: str, unit_price: float, quantity_on_hand: int =
> 0) -> None:
>       self.name = name
>       self.unit_price = unit_price
>       self.quantity_on_hand = quantity_on_hand
>   def __repr__(self):
>       return f'InventoryItem(name={self.name!r},unit_price={self.unit_pri
> ce!r},quantity_on_hand={self.quantity_on_hand!r})'
>   def __eq__(self, other):
>       if other.__class__ is self.__class__:
>           return (self.name, self.unit_price, self.quantity_on_hand) == (
> other.name, other.unit_price, other.quantity_on_hand)
>       return NotImplemented
>   def __ne__(self, other):
>       if other.__class__ is self.__class__:
>           return (self.name, self.unit_price, self.quantity_on_hand) != (
> other.name, other.unit_price, other.quantity_on_hand)
>       return NotImplemented
>   def __lt__(self, other):
>       if other.__class__ is self.__class__:
>           return (self.name, self.unit_price, self.quantity_on_hand) < (
> other.name, other.unit_price, other.quantity_on_hand)
>       return NotImplemented
>   def __le__(self, other):
>       if other.__class__ is self.__class__:
>           return (self.name, self.unit_price, self.quantity_on_hand) <= (
> other.name, other.unit_price, other.quantity_on_hand)
>       return NotImplemented
>   def __gt__(self, other):
>       if other.__class__ is self.__class__:
>           return (self.name, self.unit_price, self.quantity_on_hand) > (
> other.name, other.unit_price, other.quantity_on_hand)
>       return NotImplemented
>   def __ge__(self, other):
>       if other.__class__ is self.__class__:
>           return (self.name, self.unit_price, self.quantity_on_hand) >= (
> other.name, other.unit_price, other.quantity_on_hand)
>       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/sfermigie
> r%2Blists%40gmail.com
>



-- 
Stefane Fermigier - http://fermigier.com/ - http://twitter.com/sfermigier -
http://linkedin.com/in/sfermigier
Founder & CEO, Abilian - Enterprise Social Software -
http://www.abilian.com/
Chairman, Free&OSS Group / Systematic Cluster -
http://www.gt-logiciel-libre.org/
Co-Chairman, National Council for Free & Open Source Software (CNLL) -
http://cnll.fr/
Founder & Organiser, PyData Paris - http://pydata.fr/
---
“You never change things by fighting the existing reality. To change
something, build a new model that makes the existing model obsolete.” — R.
Buckminster Fuller
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170909/0a0bcc36/attachment.html>


More information about the Python-Dev mailing list