SlideShare a Scribd company logo
Guide to Programming with
Python
Chapter Nine
Object-Oriented Programming: The Blackjack
Game
Guide to Programming with Python 2
Objectives
• Create objects of different classes in the same
program
• Allow objects to communicate with each other
• Create more complex objects by combining simpler
ones
• Derive new classes from existing ones
• Extend the definition of existing classes
• Override method definitions of existing classes
Guide to Programming with Python 3
The Blackjack Game
Figure 9.1: Sample run of the Blackjack game
One player wins, the other is not so lucky.
Guide to Programming with Python 4
Sending and Receiving Messages
• Object-oriented program like an ecosystem
• Objects like organisms
• Organisms interact and so do objects
• Message: Communication between objects; one
object sends another a message when it invokes a
method of the other
Guide to Programming with Python 5
The Alien Blaster Program
Figure 9.2: Sample run of the Alien Blaster program
Battle description is result of objects exchanging message.
Guide to Programming with Python 6
The Alien Blaster Program (continued)
Figure 9.3: Visual representation of objects exchanging a message
hero, a Player object, sends invader, an Alien object, a message.
Guide to Programming with Python 7
The Alien Blaster Program (continued)
class Player(object):
def blast(self, enemy):
print "The player blasts an enemy."
enemy.die()
class Alien(object):
def die(self):
print "Good-bye, cruel universe."
hero = Player()
invader = Alien()
hero.blast(invader)
Guide to Programming with Python 8
Sending a Message
class Player(object):
def blast(self, enemy):
print "The player blasts an enemy."
enemy.die()
...
hero.blast(invader)
• hero.blast() method is passed invader; i.e.,
the Player blast() method is passed an Alien object
• In blast(), enemy refers to the Alien object (invader)
• blast() invokes the Alien object’s die() method; i.e.,
the Player object sends the Alien object a message
telling it to die
alien_blaster.py
Guide to Programming with Python 9
Combining Objects
• Real-world objects are often made up of other
objects
• Can mimic composition and collection in OOP
• Drag racer composed of body, tires, and engine
– Drag_Racer class with attribute engine that is a
Race_Engine object
• Zoo is collection of animals
– Zoo class that has an attribute animals which is a list
of different Animal objects
Guide to Programming with Python 10
The Playing Cards Program
Figure 9.4: Sample run of the Playing Cards program
Each Hand object has a collection of Card objects.
Guide to Programming with Python 11
Creating the Card Class
class Card(object):
""" A playing card. """
RANKS = ["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
reply = self.rank + self.suit
return reply
Guide to Programming with Python 12
Creating the Card Class (continued)
• Card object rank attribute represents rank of card
– RANKS class attribute has all possible values
– "A" ace, "J" jack, "Q" queen, "K" king, "2" - "10"
numeric values
• Card object suit attribute represents suit of card
– SUITS class attribute has all possible values
– "c" clubs, "d" diamonds, "h" hearts, "s" spades
• A Card object with rank "A" and suit "d" is the ace
of diamonds
Guide to Programming with Python 13
Creating the Hand Class
class Hand(object):
""" A hand of playing cards. """
def __init__(self):
self.cards = []
def __str__(self):
if self.cards:
reply = ""
for card in self.cards:
reply += str(card) + " "
else:
reply = "<empty>"
return reply
Guide to Programming with Python 14
Creating the Hand Class (continued)
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
Guide to Programming with Python 15
Creating the Hand Class (continued)
• Attribute
– cards is list of Card objects
• Methods
– __str__() returns string for entire hand
– clear() clears list of cards
– add() adds card to list of cards
– give() removes card from current hand and adds to
another hand
Guide to Programming with Python 16
Using Card Objects
card1 = Card(rank = "A", suit = "c")
card2 = Card(rank = "2", suit = "c")
card3 = Card(rank = "3", suit = "c")
card4 = Card(rank = "4", suit = "c")
card5 = Card(rank = "5", suit = "c")
print card1 # Ac
print card2 # 2c
print card3 # 3c
print card4 # 4c
print card5 # 5c
Guide to Programming with Python 17
Combining Card Objects Using a Hand
Object
my_hand = Hand()
print my_hand # <empty>
my_hand.add(card1)
my_hand.add(card2)
my_hand.add(card3)
my_hand.add(card4)
my_hand.add(card5)
print my_hand # Ac 2c 3c 4c 5c
Guide to Programming with Python 18
Combining Card Objects Using a Hand
Object (continued)
your_hand = Hand()
my_hand.give(card1, your_hand)
my_hand.give(card2, your_hand)
print your_hand # Ac 2c
print my_hand # 3c 4c 5c
my_hand.clear()
print my_hand # <empty>
playing_cards.py
Guide to Programming with Python 19
Using Inheritance to Create New
Classes
• Inheritance: An element of OOP that allows a new
class to be based on an existing one where the
new automatically gets (or inherits) all of the
methods and attributes of the existing class
• Like getting all the work that went into the existing
class for free
Guide to Programming with Python 20
Extending a Class Through
Inheritance
• Inheritance is used to create a more specialized
version of an existing class
• The new class gets all the methods and attributes of
the existing class
• The new class can also define additional methods and
attributes
• Drag_Racer class with methods stop() and go()
• Clean_Drag_Racer based on Drag_Racer()
– Automatically inherits stop() and go()
– Can define new method, clean(), for cleaning
windshield
Guide to Programming with Python 21
The Playing Cards 2.0 Program
Figure 9.5: Sample run of the Playing Cards 2.0 program
The Deck object inherits all of the methods of the Hand class.
Guide to Programming with Python 22
Creating a Base Class
(Same as before)
class Hand(object):
""" A hand of playing cards. """
def __init__(self):
self.cards = []
def __str__(self):
if self.cards:
reply = ""
for card in self.cards:
reply += str(card) + " "
else:
reply = "<empty>"
return rep
Guide to Programming with Python 23
Creating a Base Class (continued)
(Same as before)
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
Guide to Programming with Python 24
Inheriting from a Base Class
class Deck(Hand):
• Base class: A class upon which another is based;
it is inherited from by a derived class
• Derived class: A class that is based upon another
class; it inherits from a base class
• Hand is base class
• Deck is derived class
Guide to Programming with Python 25
Inheriting from a Base Class
(continued)
• Deck inherits Hand attribute:
cards
• Deck inherits all Hand methods:
__init__()
__str__()
clear()
add()
give()
Guide to Programming with Python 26
Extending a Derived Class
class Deck(Hand):
""" A deck of playing cards. """
def populate(self):
for suit in Card.SUITS:
for rank in Card.RANKS:
self.add(Card(rank, suit))
def shuffle(self):
import random
random.shuffle(self.cards)
Guide to Programming with Python 27
Extending a Derived Class (continued)
def deal(self, hands, per_hand = 1):
for rounds in range(per_hand):
for hand in hands:
if self.cards: # if len(self.cards) > 0
top_card = self.cards[0]
self.give(top_card, hand)
else:
print "Out of cards!"
Guide to Programming with Python 28
Extending a Derived Class (continued)
• In addition to methods Deck inherits, it defines new
methods:
populate()
shuffle()
deal()
Guide to Programming with Python 29
Using the Derived Class
deck1 = Deck()
print deck1 # <empty>
deck1.populate()
print deck1 # ordered deck
deck1.shuffle()
print deck1 # shuffled deck
Guide to Programming with Python 30
Using the Derived Class (continued)
my_hand = Hand()
your_hand = Hand()
hands = [my_hand, your_hand]
deck1.deal(hands, per_hand = 5)
print my_hand # 5 alternating cards from deck
print your_hand # 5 alternating cards from deck
print deck1 # deck minus first 10 cards
deck1.clear()
print deck1 # <empty>
playing_cards2.py
Guide to Programming with Python 31
Altering the Behavior of Inherited
Methods
• Override: To redefine how inherited method of
base class works in derived class
• Two choices when overriding
– Completely new functionality vs. overridden method
– Incorporate functionality of overridden method, add
more
Altering the Behavior of Inherited
Methods (continued)
• Drag_Racer with stop() method that applies brakes
• Parachute_Racer based on Drag_Racer overrides
stop() method, two choices
– Overridden stop() method shifts into reverse
– Overridden stop() method applies brakes (using
the overridden method) and releases parachute
Guide to Programming with Python 32
Guide to Programming with Python 33
The Playing Cards 3.0 Program
Figure 9.6: Sample run of the Playing Cards program
By overriding __str__(), objects of derived classes print differently.
Guide to Programming with Python 34
Creating a Base Class
(Same as before)
class Card(object):
""" A playing card. """
RANKS = ["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
reply = self.rank + self.suit
return reply
Guide to Programming with Python 35
Overriding Base Class Methods
class Unprintable_Card(Card):
....
def __str__(self):
return "<unprintable>"
• Unprintable_Card
– Inherits all methods of Card
– Overrides inherited method __str__()
– Printed object displayed as <unprintable>
• A derived class has no effect on a base class
Guide to Programming with Python 36
Invoking Base Class Methods
• In overridden method, can incorporate inherited method’s
functionality
• Suppose we want to have a card that can be face up or face
down…
class Positionable_Card(Card):
def __init__(self, rank, suit, face_up = True):
super(Positionable_Card, self).__init__(rank, suit)
self.is_face_up = face_up
• Superclass: Another name for a base class
• Card is the superclass of Positionable_Card
Guide to Programming with Python 37
Invoking Base Class Methods
(continued)
• Incorporate inherited method’s functionality by
calling super()
• Positionable_Card constructor invokes Card
constructor and creates new attribute
• super() lets you invoke the method of a superclass
– First argument is the base class, Positionable_Card
– Second is a reference to the object itself, self
– Last is the superclass method you want to call with
its parameters, __init__(rank, suit)
Guide to Programming with Python 38
Invoking Base Class Methods
(continued)
class Positionable_Card(Card):
def __str__(self):
if self.is_face_up:
reply = super(Positionable_Card, self).__str__()
else:
reply = "XX"
return rep
def flip(self):
self.is_face_up = not self.is_face_up
• __str__() invokes superclass __str__() method if
card is face up; otherwise, returns "XX"
Guide to Programming with Python 39
Using the Derived Classes
card1 = Card("A", "c")
card2 = Unprintable_Card("A", "d")
card3 = Positionable_Card("A", "h")
print card1 # Ac
print card2 # <unprintable>
print card3 # Ah
card3.flip()
print card3 # XX
playing_cards3.py
Guide to Programming with Python 40
Understanding Polymorphism
• Polymorphism: Aspect of object-oriented
programming that allows you to send same
message to objects of different classes, related by
inheritance, and achieve different but appropriate
results for each object
• When you invoke __str__() method of
Unprintable_Card object, you get different result
than when you invoke the __str__() method of a
Card object
Guide to Programming with Python 41
Creating Modules
• Create, use, and even share your own modules
• Reuse code
– Could reuse the Card, Hand, and Deck classes for
different card games
• Manage large projects
– Professional projects can be hundreds of thousands
of lines long
– Would be nearly impossible to maintain in one file
Guide to Programming with Python 42
The Simple Game Program
Figure 9.7: Sample run of the Simple Game program
Program uses functions and class from programmer-created module.
Guide to Programming with Python 43
Writing Modules
• Write module as a collection of related
programming components, like functions and
classes, in single file
• File is just Python file with extension .py
• Module imported using filename, just like built-in
modules
Guide to Programming with Python 44
Writing Modules (continued)
games module is file games.py
class Player(object):
""" A player for a game. """
def __init__(self, name, score = 0):
self.name = name
self.score = score
def __str__(self):
reply = self.name + ":t" + str(self.score)
return reply
Guide to Programming with Python 45
Writing Modules (continued)
def ask_yes_no(question):
"""Ask a yes or no question."""
response = None
while response not in ("y", "n"):
response = raw_input(question).lower()
return response
def ask_number(question, low, high):
"""Ask for a number within a range."""
response = None
while response not in range(low, high):
response = int(raw_input(question))
return response
Guide to Programming with Python 46
Writing Modules (continued)
if __name__ == "__main__":
print "You ran module but must 'import' it."
raw_input("nnPress the enter key to exit.")
• __name__ == "__main__" is true if file is run directly;
is false if the file is imported as module
• If games.py run directly, message displayed that the
file is meant to be imported
games.py
Guide to Programming with Python 47
Importing Modules
import games, random
• Import programmer-created module the same way
you import a built-in module
Guide to Programming with Python 48
Using Imported Functions and Classes
num = games.ask_number(question = "How many?",
low = 2, high = 5)
...
player = games.Player(name, score)
...
again = games.ask_yes_no("Play again? (y/n): ")
• Use imported programmer-created modules the
same way as you use imported built-in modules
simple_game.py
Guide to Programming with Python 49
The Blackjack Game - Classes
Table 9.1: Blackjack classes
Guide to Programming with Python 50
The Blackjack Game – Class
Hierarchy
Figure 9.8: Blackjack classes
Inheritance hierarchy of classes for the Blackjack game
Guide to Programming with Python 51
The Blackjack Game - Pseudocode
Deal each player and dealer initial two cards
For each player
While the player asks for a hit and the player is not busted
Deal the player an additional card
If there are no players still playing
Show the dealer’s two cards
Otherwise
While the dealer must hit and the dealer is not busted
Deal the dealer an additional card
If the dealer is busted
For each player who is still playing
The player wins
Guide to Programming with Python 52
The Blackjack Game – Pseudocode
(continued)
Otherwise
For each player who is still playing
If the player’s total is greater than the dealer’s total
The player wins
Otherwise, if the player’s total is less than the dealer’s total
The player loses
Otherwise
The player pushes
blackjack.py
Guide to Programming with Python 53
Summary
• In object-oriented programming, objects can send
messages to each other by invoking each other’s
methods
• Objects can be composed of other objects or have
collections of objects
• Inheritance is an aspect of object-oriented
programming that allows a new class to be based
on an existing one where the new class
automatically gets (or inherits) all of the methods
and attributes of the existing one
Summary (continued)
• Inheritance can be used to create a more
specialized version of an existing class
• A base class is a class upon which another is
based; it is inherited from by this other class (the
derived class)
• A derived class is a class that is based upon
another class; it inherits from this other class (a
base class)
• Superclass is another name for base class
Guide to Programming with Python 54
Guide to Programming with Python 55
Summary (continued)
• A derived class can define new methods and
attributes in addition to the ones that it inherits
• To override an inherited method is to redefine how
the method of a base class works in a derived
class
• When overriding a method, the new definition can
have completely different functionality than the
original definition or the new definition can
incorporate the functionality of the original
Guide to Programming with Python 56
Summary (continued)
• The super() function allows you to invoke the
method of a superclass
• Polymorphism is an aspect of object-oriented
programming that allows you to send the same
message to objects of different classes, related by
inheritance, and achieve different but appropriate
results for each object
• You can write, import, and even share your own
modules
Summary (continued)
• You write a module as a collection of related
programming components, like functions and
classes, in a single Python file
• Programmer-created modules can be imported the
same way that built-in modules are, with an import
statement
• You test to see if __name__ is equal to
"__main__" to make a module identify itself as
such
Guide to Programming with Python 57

More Related Content

PPT
week 11 inheritance week 11 inheritances
PPT
inheritance in python with full detail.ppt
PPTX
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
PPTX
PPTX
Problem solving with python programming OOP's Concept
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPTX
Object Oriented Programming.pptx
PPTX
Python programming computer science and engineering
week 11 inheritance week 11 inheritances
inheritance in python with full detail.ppt
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
Problem solving with python programming OOP's Concept
Unit 3-Classes ,Objects and Inheritance.pdf
Object Oriented Programming.pptx
Python programming computer science and engineering

Similar to Programming in python and introduction.ppt (20)

PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PPTX
Inheritance
PPTX
Python OOPs
PPTX
arthimetic operator,classes,objects,instant
PPTX
Lec-21-Classes and Object Orientation.pptx
PPTX
Class and Objects in python programming.pptx
PPTX
Advance python
PPTX
software construction and development week 3 Python lists, tuples, dictionari...
PDF
Python unit 3 m.sc cs
PDF
Object oriented Programming via python.pdf
PPTX
Software Programming with Python II.pptx
PDF
Download full ebook of Learning Python 2nd ed Edition Mark Lutz instant downl...
PPTX
OOPS 46 slide Python concepts .pptx
PPT
Introduction to Python For Diploma Students
PDF
Object-Oriented Python 1st Edition Irv Kalb
PDF
Intro to Python
PPTX
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
PPTX
Object oriented programming with python
PPTX
Object Oriented Programming - Copy.pptxb
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Inheritance
Python OOPs
arthimetic operator,classes,objects,instant
Lec-21-Classes and Object Orientation.pptx
Class and Objects in python programming.pptx
Advance python
software construction and development week 3 Python lists, tuples, dictionari...
Python unit 3 m.sc cs
Object oriented Programming via python.pdf
Software Programming with Python II.pptx
Download full ebook of Learning Python 2nd ed Edition Mark Lutz instant downl...
OOPS 46 slide Python concepts .pptx
Introduction to Python For Diploma Students
Object-Oriented Python 1st Edition Irv Kalb
Intro to Python
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
Object oriented programming with python
Object Oriented Programming - Copy.pptxb
Ad

More from ajajkhan16 (20)

PPT
Unit-4 Cybercrimes-II Mobile and Wireless Devices.ppt
PPTX
SYMMETRIC CYPHER MODELS WITH SUITABLE DIAGRAM.pptx
PPTX
6. PRESENTATION REAL TIME OBJECT DETECTION.pptx
PPTX
mini project Presentation and details of the online plateforms.pptx
PPTX
first ppt online shopping website and all.pptx
PPTX
Presentation - Smart Vigilance System.pptx
PPTX
loundry app and its advantages Final ppt.pptx
PDF
hill cipher with example and solving .pdf
PPTX
data ebncryption standard with example.pptx
PPTX
block cipher and its principle and charateristics.pptx
PDF
searchengineAND ALL ppt-171025105119.pdf
PPTX
CRAWLER,INDEX,RANKING AND ITS WORKING.pptx
PPTX
STACK 20 INTERVIEW QUESTIONS and answers for interview.pptx
PPTX
RQP reverse query processing it's application 2011.pptx
PPTX
search engine and crawler index ranking .pptx
PPTX
NoSQL 5 2_graph Database Edited - Updated.pptx.pptx
PDF
STORMPresentation and all about storm_FINAL.pdf
PDF
39.-Introduction-to-Sparkspark and all-1.pdf
PPTX
21-RDF and triplestores in NOSql database.pptx
PDF
Computer-Operator-and-Programming-Assistant-COPA-Learn-Tech-Anywhere-1.pdf
Unit-4 Cybercrimes-II Mobile and Wireless Devices.ppt
SYMMETRIC CYPHER MODELS WITH SUITABLE DIAGRAM.pptx
6. PRESENTATION REAL TIME OBJECT DETECTION.pptx
mini project Presentation and details of the online plateforms.pptx
first ppt online shopping website and all.pptx
Presentation - Smart Vigilance System.pptx
loundry app and its advantages Final ppt.pptx
hill cipher with example and solving .pdf
data ebncryption standard with example.pptx
block cipher and its principle and charateristics.pptx
searchengineAND ALL ppt-171025105119.pdf
CRAWLER,INDEX,RANKING AND ITS WORKING.pptx
STACK 20 INTERVIEW QUESTIONS and answers for interview.pptx
RQP reverse query processing it's application 2011.pptx
search engine and crawler index ranking .pptx
NoSQL 5 2_graph Database Edited - Updated.pptx.pptx
STORMPresentation and all about storm_FINAL.pdf
39.-Introduction-to-Sparkspark and all-1.pdf
21-RDF and triplestores in NOSql database.pptx
Computer-Operator-and-Programming-Assistant-COPA-Learn-Tech-Anywhere-1.pdf
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Well-logging-methods_new................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Project quality management in manufacturing
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
PPT on Performance Review to get promotions
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Well-logging-methods_new................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CH1 Production IntroductoryConcepts.pptx
Foundation to blockchain - A guide to Blockchain Tech
Internet of Things (IOT) - A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Project quality management in manufacturing
Model Code of Practice - Construction Work - 21102022 .pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
573137875-Attendance-Management-System-original
Embodied AI: Ushering in the Next Era of Intelligent Systems
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)

Programming in python and introduction.ppt

  • 1. Guide to Programming with Python Chapter Nine Object-Oriented Programming: The Blackjack Game
  • 2. Guide to Programming with Python 2 Objectives • Create objects of different classes in the same program • Allow objects to communicate with each other • Create more complex objects by combining simpler ones • Derive new classes from existing ones • Extend the definition of existing classes • Override method definitions of existing classes
  • 3. Guide to Programming with Python 3 The Blackjack Game Figure 9.1: Sample run of the Blackjack game One player wins, the other is not so lucky.
  • 4. Guide to Programming with Python 4 Sending and Receiving Messages • Object-oriented program like an ecosystem • Objects like organisms • Organisms interact and so do objects • Message: Communication between objects; one object sends another a message when it invokes a method of the other
  • 5. Guide to Programming with Python 5 The Alien Blaster Program Figure 9.2: Sample run of the Alien Blaster program Battle description is result of objects exchanging message.
  • 6. Guide to Programming with Python 6 The Alien Blaster Program (continued) Figure 9.3: Visual representation of objects exchanging a message hero, a Player object, sends invader, an Alien object, a message.
  • 7. Guide to Programming with Python 7 The Alien Blaster Program (continued) class Player(object): def blast(self, enemy): print "The player blasts an enemy." enemy.die() class Alien(object): def die(self): print "Good-bye, cruel universe." hero = Player() invader = Alien() hero.blast(invader)
  • 8. Guide to Programming with Python 8 Sending a Message class Player(object): def blast(self, enemy): print "The player blasts an enemy." enemy.die() ... hero.blast(invader) • hero.blast() method is passed invader; i.e., the Player blast() method is passed an Alien object • In blast(), enemy refers to the Alien object (invader) • blast() invokes the Alien object’s die() method; i.e., the Player object sends the Alien object a message telling it to die alien_blaster.py
  • 9. Guide to Programming with Python 9 Combining Objects • Real-world objects are often made up of other objects • Can mimic composition and collection in OOP • Drag racer composed of body, tires, and engine – Drag_Racer class with attribute engine that is a Race_Engine object • Zoo is collection of animals – Zoo class that has an attribute animals which is a list of different Animal objects
  • 10. Guide to Programming with Python 10 The Playing Cards Program Figure 9.4: Sample run of the Playing Cards program Each Hand object has a collection of Card objects.
  • 11. Guide to Programming with Python 11 Creating the Card Class class Card(object): """ A playing card. """ RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] SUITS = ["c", "d", "h", "s"] def __init__(self, rank, suit): self.rank = rank self.suit = suit def __str__(self): reply = self.rank + self.suit return reply
  • 12. Guide to Programming with Python 12 Creating the Card Class (continued) • Card object rank attribute represents rank of card – RANKS class attribute has all possible values – "A" ace, "J" jack, "Q" queen, "K" king, "2" - "10" numeric values • Card object suit attribute represents suit of card – SUITS class attribute has all possible values – "c" clubs, "d" diamonds, "h" hearts, "s" spades • A Card object with rank "A" and suit "d" is the ace of diamonds
  • 13. Guide to Programming with Python 13 Creating the Hand Class class Hand(object): """ A hand of playing cards. """ def __init__(self): self.cards = [] def __str__(self): if self.cards: reply = "" for card in self.cards: reply += str(card) + " " else: reply = "<empty>" return reply
  • 14. Guide to Programming with Python 14 Creating the Hand Class (continued) def clear(self): self.cards = [] def add(self, card): self.cards.append(card) def give(self, card, other_hand): self.cards.remove(card) other_hand.add(card)
  • 15. Guide to Programming with Python 15 Creating the Hand Class (continued) • Attribute – cards is list of Card objects • Methods – __str__() returns string for entire hand – clear() clears list of cards – add() adds card to list of cards – give() removes card from current hand and adds to another hand
  • 16. Guide to Programming with Python 16 Using Card Objects card1 = Card(rank = "A", suit = "c") card2 = Card(rank = "2", suit = "c") card3 = Card(rank = "3", suit = "c") card4 = Card(rank = "4", suit = "c") card5 = Card(rank = "5", suit = "c") print card1 # Ac print card2 # 2c print card3 # 3c print card4 # 4c print card5 # 5c
  • 17. Guide to Programming with Python 17 Combining Card Objects Using a Hand Object my_hand = Hand() print my_hand # <empty> my_hand.add(card1) my_hand.add(card2) my_hand.add(card3) my_hand.add(card4) my_hand.add(card5) print my_hand # Ac 2c 3c 4c 5c
  • 18. Guide to Programming with Python 18 Combining Card Objects Using a Hand Object (continued) your_hand = Hand() my_hand.give(card1, your_hand) my_hand.give(card2, your_hand) print your_hand # Ac 2c print my_hand # 3c 4c 5c my_hand.clear() print my_hand # <empty> playing_cards.py
  • 19. Guide to Programming with Python 19 Using Inheritance to Create New Classes • Inheritance: An element of OOP that allows a new class to be based on an existing one where the new automatically gets (or inherits) all of the methods and attributes of the existing class • Like getting all the work that went into the existing class for free
  • 20. Guide to Programming with Python 20 Extending a Class Through Inheritance • Inheritance is used to create a more specialized version of an existing class • The new class gets all the methods and attributes of the existing class • The new class can also define additional methods and attributes • Drag_Racer class with methods stop() and go() • Clean_Drag_Racer based on Drag_Racer() – Automatically inherits stop() and go() – Can define new method, clean(), for cleaning windshield
  • 21. Guide to Programming with Python 21 The Playing Cards 2.0 Program Figure 9.5: Sample run of the Playing Cards 2.0 program The Deck object inherits all of the methods of the Hand class.
  • 22. Guide to Programming with Python 22 Creating a Base Class (Same as before) class Hand(object): """ A hand of playing cards. """ def __init__(self): self.cards = [] def __str__(self): if self.cards: reply = "" for card in self.cards: reply += str(card) + " " else: reply = "<empty>" return rep
  • 23. Guide to Programming with Python 23 Creating a Base Class (continued) (Same as before) def clear(self): self.cards = [] def add(self, card): self.cards.append(card) def give(self, card, other_hand): self.cards.remove(card) other_hand.add(card)
  • 24. Guide to Programming with Python 24 Inheriting from a Base Class class Deck(Hand): • Base class: A class upon which another is based; it is inherited from by a derived class • Derived class: A class that is based upon another class; it inherits from a base class • Hand is base class • Deck is derived class
  • 25. Guide to Programming with Python 25 Inheriting from a Base Class (continued) • Deck inherits Hand attribute: cards • Deck inherits all Hand methods: __init__() __str__() clear() add() give()
  • 26. Guide to Programming with Python 26 Extending a Derived Class class Deck(Hand): """ A deck of playing cards. """ def populate(self): for suit in Card.SUITS: for rank in Card.RANKS: self.add(Card(rank, suit)) def shuffle(self): import random random.shuffle(self.cards)
  • 27. Guide to Programming with Python 27 Extending a Derived Class (continued) def deal(self, hands, per_hand = 1): for rounds in range(per_hand): for hand in hands: if self.cards: # if len(self.cards) > 0 top_card = self.cards[0] self.give(top_card, hand) else: print "Out of cards!"
  • 28. Guide to Programming with Python 28 Extending a Derived Class (continued) • In addition to methods Deck inherits, it defines new methods: populate() shuffle() deal()
  • 29. Guide to Programming with Python 29 Using the Derived Class deck1 = Deck() print deck1 # <empty> deck1.populate() print deck1 # ordered deck deck1.shuffle() print deck1 # shuffled deck
  • 30. Guide to Programming with Python 30 Using the Derived Class (continued) my_hand = Hand() your_hand = Hand() hands = [my_hand, your_hand] deck1.deal(hands, per_hand = 5) print my_hand # 5 alternating cards from deck print your_hand # 5 alternating cards from deck print deck1 # deck minus first 10 cards deck1.clear() print deck1 # <empty> playing_cards2.py
  • 31. Guide to Programming with Python 31 Altering the Behavior of Inherited Methods • Override: To redefine how inherited method of base class works in derived class • Two choices when overriding – Completely new functionality vs. overridden method – Incorporate functionality of overridden method, add more
  • 32. Altering the Behavior of Inherited Methods (continued) • Drag_Racer with stop() method that applies brakes • Parachute_Racer based on Drag_Racer overrides stop() method, two choices – Overridden stop() method shifts into reverse – Overridden stop() method applies brakes (using the overridden method) and releases parachute Guide to Programming with Python 32
  • 33. Guide to Programming with Python 33 The Playing Cards 3.0 Program Figure 9.6: Sample run of the Playing Cards program By overriding __str__(), objects of derived classes print differently.
  • 34. Guide to Programming with Python 34 Creating a Base Class (Same as before) class Card(object): """ A playing card. """ RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] SUITS = ["c", "d", "h", "s"] def __init__(self, rank, suit): self.rank = rank self.suit = suit def __str__(self): reply = self.rank + self.suit return reply
  • 35. Guide to Programming with Python 35 Overriding Base Class Methods class Unprintable_Card(Card): .... def __str__(self): return "<unprintable>" • Unprintable_Card – Inherits all methods of Card – Overrides inherited method __str__() – Printed object displayed as <unprintable> • A derived class has no effect on a base class
  • 36. Guide to Programming with Python 36 Invoking Base Class Methods • In overridden method, can incorporate inherited method’s functionality • Suppose we want to have a card that can be face up or face down… class Positionable_Card(Card): def __init__(self, rank, suit, face_up = True): super(Positionable_Card, self).__init__(rank, suit) self.is_face_up = face_up • Superclass: Another name for a base class • Card is the superclass of Positionable_Card
  • 37. Guide to Programming with Python 37 Invoking Base Class Methods (continued) • Incorporate inherited method’s functionality by calling super() • Positionable_Card constructor invokes Card constructor and creates new attribute • super() lets you invoke the method of a superclass – First argument is the base class, Positionable_Card – Second is a reference to the object itself, self – Last is the superclass method you want to call with its parameters, __init__(rank, suit)
  • 38. Guide to Programming with Python 38 Invoking Base Class Methods (continued) class Positionable_Card(Card): def __str__(self): if self.is_face_up: reply = super(Positionable_Card, self).__str__() else: reply = "XX" return rep def flip(self): self.is_face_up = not self.is_face_up • __str__() invokes superclass __str__() method if card is face up; otherwise, returns "XX"
  • 39. Guide to Programming with Python 39 Using the Derived Classes card1 = Card("A", "c") card2 = Unprintable_Card("A", "d") card3 = Positionable_Card("A", "h") print card1 # Ac print card2 # <unprintable> print card3 # Ah card3.flip() print card3 # XX playing_cards3.py
  • 40. Guide to Programming with Python 40 Understanding Polymorphism • Polymorphism: Aspect of object-oriented programming that allows you to send same message to objects of different classes, related by inheritance, and achieve different but appropriate results for each object • When you invoke __str__() method of Unprintable_Card object, you get different result than when you invoke the __str__() method of a Card object
  • 41. Guide to Programming with Python 41 Creating Modules • Create, use, and even share your own modules • Reuse code – Could reuse the Card, Hand, and Deck classes for different card games • Manage large projects – Professional projects can be hundreds of thousands of lines long – Would be nearly impossible to maintain in one file
  • 42. Guide to Programming with Python 42 The Simple Game Program Figure 9.7: Sample run of the Simple Game program Program uses functions and class from programmer-created module.
  • 43. Guide to Programming with Python 43 Writing Modules • Write module as a collection of related programming components, like functions and classes, in single file • File is just Python file with extension .py • Module imported using filename, just like built-in modules
  • 44. Guide to Programming with Python 44 Writing Modules (continued) games module is file games.py class Player(object): """ A player for a game. """ def __init__(self, name, score = 0): self.name = name self.score = score def __str__(self): reply = self.name + ":t" + str(self.score) return reply
  • 45. Guide to Programming with Python 45 Writing Modules (continued) def ask_yes_no(question): """Ask a yes or no question.""" response = None while response not in ("y", "n"): response = raw_input(question).lower() return response def ask_number(question, low, high): """Ask for a number within a range.""" response = None while response not in range(low, high): response = int(raw_input(question)) return response
  • 46. Guide to Programming with Python 46 Writing Modules (continued) if __name__ == "__main__": print "You ran module but must 'import' it." raw_input("nnPress the enter key to exit.") • __name__ == "__main__" is true if file is run directly; is false if the file is imported as module • If games.py run directly, message displayed that the file is meant to be imported games.py
  • 47. Guide to Programming with Python 47 Importing Modules import games, random • Import programmer-created module the same way you import a built-in module
  • 48. Guide to Programming with Python 48 Using Imported Functions and Classes num = games.ask_number(question = "How many?", low = 2, high = 5) ... player = games.Player(name, score) ... again = games.ask_yes_no("Play again? (y/n): ") • Use imported programmer-created modules the same way as you use imported built-in modules simple_game.py
  • 49. Guide to Programming with Python 49 The Blackjack Game - Classes Table 9.1: Blackjack classes
  • 50. Guide to Programming with Python 50 The Blackjack Game – Class Hierarchy Figure 9.8: Blackjack classes Inheritance hierarchy of classes for the Blackjack game
  • 51. Guide to Programming with Python 51 The Blackjack Game - Pseudocode Deal each player and dealer initial two cards For each player While the player asks for a hit and the player is not busted Deal the player an additional card If there are no players still playing Show the dealer’s two cards Otherwise While the dealer must hit and the dealer is not busted Deal the dealer an additional card If the dealer is busted For each player who is still playing The player wins
  • 52. Guide to Programming with Python 52 The Blackjack Game – Pseudocode (continued) Otherwise For each player who is still playing If the player’s total is greater than the dealer’s total The player wins Otherwise, if the player’s total is less than the dealer’s total The player loses Otherwise The player pushes blackjack.py
  • 53. Guide to Programming with Python 53 Summary • In object-oriented programming, objects can send messages to each other by invoking each other’s methods • Objects can be composed of other objects or have collections of objects • Inheritance is an aspect of object-oriented programming that allows a new class to be based on an existing one where the new class automatically gets (or inherits) all of the methods and attributes of the existing one
  • 54. Summary (continued) • Inheritance can be used to create a more specialized version of an existing class • A base class is a class upon which another is based; it is inherited from by this other class (the derived class) • A derived class is a class that is based upon another class; it inherits from this other class (a base class) • Superclass is another name for base class Guide to Programming with Python 54
  • 55. Guide to Programming with Python 55 Summary (continued) • A derived class can define new methods and attributes in addition to the ones that it inherits • To override an inherited method is to redefine how the method of a base class works in a derived class • When overriding a method, the new definition can have completely different functionality than the original definition or the new definition can incorporate the functionality of the original
  • 56. Guide to Programming with Python 56 Summary (continued) • The super() function allows you to invoke the method of a superclass • Polymorphism is an aspect of object-oriented programming that allows you to send the same message to objects of different classes, related by inheritance, and achieve different but appropriate results for each object • You can write, import, and even share your own modules
  • 57. Summary (continued) • You write a module as a collection of related programming components, like functions and classes, in a single Python file • Programmer-created modules can be imported the same way that built-in modules are, with an import statement • You test to see if __name__ is equal to "__main__" to make a module identify itself as such Guide to Programming with Python 57