Python Reading
Key Ideas
- 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. What are three methods that each turtle has? What are three aspects of a turtle's state?
In-Class Activity
- Take a look at date.py.
- What are the state variables of the Date class?
- What are the methods of the Date class?
- Terminology: The __init__ method is called a constructor.
- Terminology: The get_month, get_day and get_year methods are called reader methods.
- Terminology: The set_day method is called a writer method.
- Terminology: Another name for a state variable is an instance variable.
- Are there any aspects of date.py that you do not understand?
Active Learning
- Think of a real world object (ie: date, point, car, pair of skis, dog, coffee cup, playlist, etc...) Consider: how could this object be modeled in an Object Oriented Programming language -- like Python?! No matter what object you think of, divide the model into these two parts:
- State (ie: data; state data,;state variables; instance variables; properties; attributes
- Methods (ie: operations; class/object functions; behaviors; etc.)
- Model your object in Python using the class definition in python
- Give your class an uppercase name, like:
class Date:, classPoint, class Car:, class Pair_of_skis:, etc.
- Use Python's
def __init__(x, y, z, etc) constructor method to initialize all of the starting data you want your object to have (it can change later -- it's mutable!)
- Use Python's
def yourfunction(self, your_obj_data) method definitions to act on your object's data.