June 2019
Beginner to intermediate
770 pages
19h 24m
English
Note the structure of the if statement in the card() function. We did not use a catch-all else clause to do any processing; we merely raised an exception. The use of a catch-all else clause is subject to debate.
On the one hand, it can be argued that the condition that belongs in an else clause should never be left unstated because it may hide subtle design errors. On the other hand, some else clause conditions are truly obvious.
It's important to avoid a vague else clause.
Consider the following variant on this factory function definition:
def card2(rank: int, suit: Suit) -> Card: if rank == 1: return AceCard("A", suit) elif 2 <= rank < 11: return Card(str(rank), suit) else: name = {11: "J" ...
Read now
Unlock full access