(original) (raw)

Interesting. I note that this under "Specification":

"""
field's may optionally specify a default value, using normal Python syntax:

@dataclass
class C:
int a # 'a' has no default value
int b = 0 # assign a default value for 'b'
"""

...does not look like "normal Python syntax".

On Fri, Sep 8, 2017 at 11:44 AM Eric V. Smith <eric@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
\> 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\_price!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@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/jcgoble3%40gmail.com