Object Oriented Basics
Reading
Chapter 16.
Key ideas
From chapter 16
- Thus far, we have used the procedural paradigm to solve problems. With the procedural paradigm, the focus on solving a problem is on the actions. In Python, these actions are implemented with functions.
- Python also supports the object-oriented programming paradigm. With the OOP paradigm, the focus on solving a problem is on the objects. An object contains both data (called the state) and functionality (called methods).
- Example: Consider the turtle module. This module is implemented using object-oriented programming.
- Example: date.py
- Functions can take objects as parameters.
- Functions can return objects.
- Method
__str__
can be used to override how objects are printed, e.g.print(object)
. - It is common to split classes into their own files and import them.
Active learning
Activity 1
Create a class named Pokemon that has the following instance variables: number, name and combat_points. Add a constructor method that enables a new Pokémon to be created. Add methods named get_name, get_number, get_combat_points and set_combat_points. Then, write a program that utilizes the functionality of the Pokémon class in its entirety.
Activity 2
In the game of Black Jack, an ace is worth 11 points, a king is worth 10 points, a queen is worth 10 points, a jack is worth 10 points, and all other cards are worth face value. Download card.py and blackjack.py onto your local machine. Complete method assign_value
and function process_hand
such that the following output is produced when the program is run.
ace of spades, king of diamonds evaluates to 21
queen of hearts, ace of spades evaluates to 21
ace of spades, jack of clubs evaluates to 21
ten of spades, ace of spades evaluates to 21
two of spades, three of clubs, four of diamonds, five of hearts, six of spades, seven of clubs evaluates to 27
eight of diamonds, nine of hearts, two of spades evaluates to 19
Activity 3
Download carddeck.py.
- With your partner, answer the following questions:
- What is
cards
in the program? - What does the
Deck
constructor do?
- What is
- Uncomment the two commented statements at the bottom and supply the missing shuffle method.
- Split the solution into three files: Card.py, Deck.py and carddeck.py.