newb question about @property
Steve D'Aprano
steve+python at pearwood.info
Thu Oct 5 09:13:55 EDT 2017
On Thu, 5 Oct 2017 10:51 pm, bartc wrote:
> Am I allowed to say that it all seems a bit of a mess?
You may or may not be pleased to learn that there's a push to create a "record
like" or "struct like" datatype for Python 3.7 or 3.8, tentatively called
a "Data Class" for now.
The proposed syntax will use class syntax, to make it easy to add methods. For
example:
@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
which will fill in the boilerplate for you:
- an appropriate initialiser __init__
- a good-looking __repr__
- equality, inequality, and rich comparison operators;
- an optional __hash__ method.
https://www.python.org/dev/peps/pep-0557/
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list