Module 4
Module 4
• This code makes it easy to compare cards; because higher suits map to
higher numbers, we can compare suits by comparing their codes.
• The mapping for ranks is fairly obvious; each of the numerical ranks maps
to the corresponding integer, and for face cards.
• The → symbol to make it clear that these mappings are not part of the
Python program. They are part of the program design, but they don’t
appear explicitly in the code.
Card Objects
• The class definition
• n for Card looks like this:
• As usual, the init method takes an optional parameter for each attribute.
The default card is the 2 of Clubs.
• To create a Card, we call Card with the suit and rank of the card you want
2. Class Attributes
• In order to print Card, we need a mapping from the integer codes to the
corresponding ranks and suits. A natural way to do that is with lists of strings. We
assign these lists to class attributes:
• Variables like suit_names and rank_names, which are defined inside a class but
outside of any method, are called class attributes because they are associated
with the class object Card.
• The variables like suit and rank, which are called instance attributes because they
are associated with a particular instance.
• Both kinds of attribute are accessed using dot notation. Class
o Ex, in __str__, self is a Card object, and self.rank is its rank. Attributes
o Similarly, Card is a class object, and Card.rank_names is a list of strings
associated with the class.
• The first element of rank_names is None because there is no card
with rank zero.
• By including None as a place-keeper, we get a mapping with the
nice property that the index 2 maps to the string '2', and so on. To
avoid this tweak, we could have used a dictionary instead of a list.
With the methods we have so far, we can create and print cards:
3. Comparing Cards
• For built-in types, there are relational operators (, ==, etc.) that compare values
and determine when one is greater than, less than, or equal to another.
• For user-defined types, we can override the behavior of the built-in operators by
providing a method named __cmp__
• __cmp__ takes two parameters, self and other, and returns a positive number if
the first object is greater, a negative number if the second object is greater, and 0
if they are equal to each other.
• The built-in function cmp has the same interface as the method __cmp__: it takes
two values and returns a positive number if the first is larger, a negative number if
the second is larger, and 0 if they are equal.
• In Python 3, cmp no longer exists, and the __cmp__ method is not supported.
Instead you should provide __lt__, which returns True if self is less than other. You
can implement __lt__ using tuples and the < operator.
Comparing Cards
4. Decks
• Now that we have Cards, the next step is to define Decks. Since a deck is made up
of cards, it is natural for each Deck to contain a list of cards as an attribute.
• The init method creates the attribute cards and generates the standard set of fifty-
two cards:
• A method like this that uses another function without doing much
real work is sometimes calleda veneer-(where it is common to glue a
thin layer of good quality wood to the surface of a cheaper piece of
wood).
• In this case we are defining a “thin” method that expresses a list
operation in terms that are appropriate for decks.
Add, Remove, Shuffle and Sort
Shuffle
• We can write a Deck method named shuffle using the function shuffle
from the random module:
• Don’t forget to import random.
7.Inheritance
• The language feature most often associated with object-oriented programming
is inheritance.
• Inheritance is the ability to define a new class that is a modified version of an
existing class.
• It is called “inheritance” because the new class inherits the methods of the
existing class.
• Extending this metaphor, the existing class is called the parent and the new
class is called the child.
• The definition of a child class is like other class definitions, but the name of the
parent class appears in parentheses:
Inheritance
• This definition indicates that Hand inherits from Deck; that means we
can use methods like pop_card and add_card for Hands as well as
Decks.
• Inheritance is a useful feature
• Inheritance can facilitate code reuse, since you can customize the
behavior of parent classes without having to modify them.
• In some cases, the inheritance structure reflects the natural structure
of the problem, which makes the program easier to understand.
8. Class Diagrams
• A class diagram is a more abstract representation of the structure of a
program. Instead of showing individual objects, it shows classes and
the relationships between them.
• There are several kinds of relationship between classes:
• Objects in one class might contain references to objects in another class. For
example, each Rectangle contains a reference to a Point, and each Deck
contains references to many Cards. This kind of relationship is called HAS-A,
as in, “a Rectangle has a Point.”
• One class might inherit from another. This relationship
is called IS-A, as in, “a Hand is a kind of a Deck.”
• One class might depend on another in the sense that
changes in one class would require changes in the other.
Class Diagrams
• A class diagram is a graphical representation of these relationships.
For example, the above class diagram shows the relationships
between Card, Deck and Hand.
• The arrow with a hollow triangle head represents an IS-A relationship;
in this case it indicates that Hand inherits from Deck.
• The standard arrow head represents a HAS-A relationship; in this
case a Deck has references to Card objects.
• The star (*) near the arrow head is a multiplicity; it indicates how
many Cards a Deck has. A multiplicity can be a simple number, like 52,
a range, like 5..7 or a star, which indicates that a Deck can have any
number of Cards.