OOP
OOP
Scope Resolution
Scopes have a hierarchy.
Use a Module
Import the module named my_module, and
call the greeting_student function:
What Is a File?
A file is a software object that allows a
program to represent and access data stored
in secondary memory
You can create an alias when you import a Text files: for storing and accessing text
module, by using the as keyword: (characters)
Binary files: executable programs and their
data files (such as images, sound clips,
video)
Text Files
Import From Module
A text file is logically a sequence of
You can choose to import only parts from a characters
module, by using the from keyword. Basic operations are
o input (read characters from the
file)
o output (write characters to the file)
There are several flavors of each type of
operation
File Input
Lesson 10: Files
- We want to bring text in from a file for processing
Three steps:
Open the file for input
Read the text and save it in a variable
Process the text Extract a substring up to but not including
the last character (the newline)
This will produce an exact echo of the input
Opening a File file
Output: Hello
Sample Program
Nothing went wrong
Raise an exception
As a Python developer, you can choose to
throw an exception if a condition occurs.
To throw (or raise) an exception, use the
“raise” keyword.
Example
Lesson 11: Try-Except
Raise an error and stop the program if x is As problems get more interesting and
lower than 0: difficult, solutions (programs) become more
complex
How do we control this complexity, so as to
keep it manageable and not be overwhelmed
by it?
Example: Dice
Dice are used in many games of chance
(backgammon, monopoly, etc.)
A single die is a cube whose sides represent
the numbers 1-6
When a die is rolled, the number selected is
the one on the side that happens to be facing
Turtle, Point, and Circle are classes
up
myrtle, yertle, p, c1, and c2 refer to objects
or instances of these classes
A class defines the behavior of a set of
objects State and Behavior of a Die
The state of a die is the number currently
visible on its top face; its initial state is a
Getting Objects to Do Things randomly chosen number from 1 through 6
Behavior:
o Roll the die to reset its state to a
randomly chosen number from 1
through 6
o Get the die’s current number
Verbs in the description indicate
behavior; nouns indicate attributes (state)
Modeling
Object Instantiation
Calling a Method
The Die Class: Its Interface and Use Defining the Die Class
Interface
The name self must appear as the
first parameter in each instance
method definition
We’ll use random.randint to roll the die Python uses this parameter to refer to
the object on which the method is
called
The Class Header
Instance Variables
Class Variables
An instance variable refers to storage
owned by a single instance
A class variable refers to storage owned by
the class, and thus available to all of its
instances
Encapsulation
Restricting the manipulation of an object’s
state by external users to a set of method
calls.
Polymorphism
Allowing several different classes to use the
same general method names.
Python supports this capability with
polymorphic methods.
The term polymorphic means “many
bodies,” and it applies to two methods that
have the same header but have different
definitions in different classes.
Inheritance
Inheritance allows us to define a class that
inherits all the methods and properties from
another class.
Parent class is the class being inherited
from, also called base class/superclass.
Child class is the class that inherits from
another class, also called derived
class/subclass.