June 2019
Beginner to intermediate
770 pages
19h 24m
English
As we saw previously, the output from __str__() and __repr__() is not very informative. We'll almost always need to override them. The following is an approach to override __str__() and __repr__() when there's no collection involved. These methods belong to the Card class, defined previously as follows:
def __repr__(self) -> str: return f"{self.__class__.__name__}(suit={self.suit!r}, rank={self.rank!r})"def __str__(self) -> str: return f"{self.rank}{self.suit}"
These two methods rely on f-strings to interpolate values from the instance into a string template. In the __repr__() method, the class name, suit, and rank were used to create a string that could be used to rebuild the object. In the __str__() method, ...
Read now
Unlock full access