[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

newb question about @property

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

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.


?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure
enough, things got worse.