Title: Introduction to Python
1Introduction to Python
- Object Oriented Programming
2Basics
- Motivation
- Reuse code
- Separate implementation details from interface
- Idea
- Model code after real-world objects
- Group software into classes
- Encapsulate information into an object
3Abstraction
- Hide implementation details
- Only expose the interface
- Change the implementation without affecting
interface
Interface
Implementation
4Characteristics
- Objects have 2 major characteristics
- State
- A set of data encapsulated by the object
- The variables containing the objects current
state - Behavior
- A set of methods which affect the state of the
object - The procedures/functions which operate on the data
5State
- The car has a state (set of variables or members)
- The amount of gas in the tank
- The pressure of the break on the tire
- The amount of gas going into the engine
- The angle of the tires
6Behaviour
- There are ways to change the behaviour of the car
- Add gas to the tank
- Step on the break pedal
- Step on the gas pedal
- Turn the steering wheel
- These methods make up the interface
7Classes
- A class is the blueprint for an object
- Defines how to create the object
- Defines the interface
- Defines the state variables to be used
Class
Object
8Objects
- An object is an instance of a class
- There can be many instances of a class
- Each instance has its own state
- Changing one instances does not affect others
9Car Interface
- The cars interface can be expressed in code
Methods
Members
Accelerate(amount) Decelerate(amount) AddGas(amoun
t) Steer(direction, amount) GetSpeed() GetRemainin
gGas()
gas currentWheelDirection amountAcceleration amoun
tDeceleration previousSpeed
10Creating Objects
- All objects must be explicitly created
- Initialization takes place in the constructor
- Variables can be passed to the constructor to
control how the object is created - When the object is no longer needed it is deleted
by the destructor - Python does not require user-defined destructors
- Python Example
- car1 Car("Firebird", "red")
- car2 Car("Blazer", "silver")
- car1.Accelerate(2)
- print car1.GetSpeed()
- 10 m.p.h.
- print car2.GetSpeed()
- 0 m.p.h.
11Using Objects
- Methods are called on instances
- The class name cannot be used to call methods
- Example
- car1 Car("Firebird", "red")
- Car.Accelerate(2) Error! Car is the name
of the class - car1.Accelerate(2) Correct! car1 is an
instance of Car
This is not wrong if the method is a static
method
12Advantages
- Code Reuse
- By packaging code into a class, code can be more
easily shared - Abstraction
- Details of the implementation can be changed
without affecting client code - Simplicity
- Simplify complex tasks by creating a simpler
interface
13Turtle Class
- Example Class Turtle
- A Logo-like drawing tool
- The turtle can rotate in any direction and draw
straight lines
14Turtle Class
- Basic Interface
- DrawForward(num)
- MoveForward(num)
- Rotate(amount, direction, units)
- Basic Members
- currentPosition
- currentAngle
- canvas
15Turtle Class
currentPosition (50, 6) currentAngle 0.785
16Turtle Class
- Advanced Interface
- SaveState()
- RecoverState()
- SetColor(color)
- SetThickness(thickness)
- Advanced Members
- stateStack
- currentColor
- currentThickness
17Turtle Example
import TurtleDraw myTurtle TurtleDraw.Turtle()
Construct a Turtle object myTurtle.DrawForward(1
00) myTurtle.SaveState() Save current
position and angle myTurtle.Rotate(45) myTurtle.Dr
awForward(75) myTurtle.RecoverState() Return
to saved position and angle myTurtle.Rotate(-45) m
yTurtle.DrawForward(75) TurtleDraw.run()
Required by Tkinter to draw
18Turtle Example
19Multiple Turtles
- Multiple turtles can be created
- Each has its own members and state
- Each is controlled separately
20Multiple Turtle Example
import TurtleDraw Create two different
Turtles redTurtle TurtleDraw.Turtle(color'red')
blueTurtle TurtleDraw.Turtle(color'blue') red
Turtle.Rotate(90) blueTurtle.Rotate(-90) redTurtl
e.DrawForward(100) blueTurtle.DrawForward(50) Tur
tleDraw.run() Required by Tkinter to draw