Introduction to Python - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Introduction to Python

Description:

Turn the steering wheel. These methods make up the interface. Behaviour ... Steer(direction, amount) GetSpeed() GetRemainingGas() gas. currentWheelDirection ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 21
Provided by: chadh8
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Python


1
Introduction to Python
  • Object Oriented Programming

2
Basics
  • Motivation
  • Reuse code
  • Separate implementation details from interface
  • Idea
  • Model code after real-world objects
  • Group software into classes
  • Encapsulate information into an object

3
Abstraction
  • Hide implementation details
  • Only expose the interface
  • Change the implementation without affecting
    interface

Interface
Implementation
4
Characteristics
  • 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

5
State
  • 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

6
Behaviour
  • 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

7
Classes
  • 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
8
Objects
  • 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

9
Car 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
10
Creating 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.

11
Using 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
12
Advantages
  • 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

13
Turtle Class
  • Example Class Turtle
  • A Logo-like drawing tool
  • The turtle can rotate in any direction and draw
    straight lines

14
Turtle Class
  • Basic Interface
  • DrawForward(num)
  • MoveForward(num)
  • Rotate(amount, direction, units)
  • Basic Members
  • currentPosition
  • currentAngle
  • canvas

15
Turtle Class
currentPosition (50, 6) currentAngle 0.785
16
Turtle Class
  • Advanced Interface
  • SaveState()
  • RecoverState()
  • SetColor(color)
  • SetThickness(thickness)
  • Advanced Members
  • stateStack
  • currentColor
  • currentThickness

17
Turtle 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
18
Turtle Example
19
Multiple Turtles
  • Multiple turtles can be created
  • Each has its own members and state
  • Each is controlled separately

20
Multiple 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
Write a Comment
User Comments (0)
About PowerShow.com