Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 643 Bytes

soln3_6.md

File metadata and controls

34 lines (23 loc) · 643 Bytes

Exercise 3.6 - Solution

(a) Better output for printing objects

# stock.py

class Stock:
    ...

    def __repr__(self):
        # Note: The !r format code produces the repr() string
        return f'{type(self).__name__}({self.name!r}, {self.shares!r}, {self.price!r})'
    ...

(b) Making Objects Comparable

class Stock:
    ...
    def __eq__(self, other):
        return isinstance(other, Stock) and ((self.name, self.shares, self.price) ==
                                 (other.name, other.shares, other.price))
    ...

(c) Context Managers

Code is given in the exercise.

Back