06-intro-object-oriented-prog1 barb ericson georgia institute of technology july 2009 introduction...
DESCRIPTION
06-Intro-Object-Oriented-Prog3 Procedural Programming Focus is on the procedures –The tasks that need to be done –Break large tasks into smaller tasks Write a function for each task Problems with procedural –The data is passed around and any function can change the data –It is hard to find the function They can be in many files with many namesTRANSCRIPT
![Page 1: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/1.jpg)
06-Intro-Object-Oriented-Prog 1
Barb EricsonGeorgia Institute of Technology
July 2009
Introduction to Object-Oriented Programming in Python
![Page 2: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/2.jpg)
06-Intro-Object-Oriented-Prog 2
Learning Goals
• Compare procedural programming and object-oriented programming
• Introduce computation as simulation• Create objects in Python• Invoke methods on objects in Python• Create a subclass• Create a method in a class• Pass a parameter to a method in a class
![Page 3: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/3.jpg)
06-Intro-Object-Oriented-Prog 3
Procedural Programming
• Focus is on the procedures– The tasks that need to be done– Break large tasks into smaller tasks
• Write a function for each task
• Problems with procedural– The data is passed around and any function
can change the data– It is hard to find the function
• They can be in many files with many names
![Page 4: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/4.jpg)
06-Intro-Object-Oriented-Prog 4
Object-Oriented Programming• The focus is on who does each task as
well as the tasks to be done– Responsibility driven
• Advantages– Manage complexity by distributing
responsibility across objects– Make systems robust by making objects work
independently– Support reuse because objects provide
services
![Page 5: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/5.jpg)
06-Intro-Object-Oriented-Prog 5
Computers as Simulators
• “The computer is the Proteus of machines. Its essence is its universality, its power to simulate. Because it can take on a thousand forms and serve a thousand functions, it can appeal to a thousand tastes.” Seymour Papert in Mindstorms
![Page 6: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/6.jpg)
06-Intro-Object-Oriented-Prog 6
Creating a Simulation
• Computers let us simulate things– We do this by creating models of the things
we want to simulate– We need to define what types of objects we
will want in our simulation and what they can do
• Classes define the types and create objects of that type
• Objects act in the simulation
![Page 7: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/7.jpg)
06-Intro-Object-Oriented-Prog 7
Creating objects
• We have been creating objects in Python using– makePicture(file)– makeSound(file)
• But, these are functions we created to make it easy to create objects from these classes
![Page 8: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/8.jpg)
06-Intro-Object-Oriented-Prog 8
Creating objects in python• Use ClassName(value1,value2,…)
– Picture(file) # create a picture– Sound(file) # create a sound– World() # create a world
• Name the objects that are created>>> earth = World()>>> tina = Turtle(earth)>>> sue = Turtle(earth)
• Use the name to ask the object to do something objName.function()>>> tina.forward()
![Page 9: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/9.jpg)
06-Intro-Object-Oriented-Prog 9
Turtle Behaviors• forward() # forward by 100 pixels• forward(amount) # forward by amount• turnLeft() # 90 degree left• turnRight() # 90 degree right• turn(degrees) # pos right and neg is left • penUp() # pick up the pen• penDown() # put pen down• moveTo(x,y) # move to x and y location• setColor(blue) # set the color• setPenWidth(width) # size of pen trail• setVisible(false) # don't draw turtle
![Page 10: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/10.jpg)
06-Intro-Object-Oriented-Prog 10
Challenge
• Use a turtle to draw a square• Use a turtle to draw blocks of color using a
wide pen trail
![Page 11: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/11.jpg)
06-Intro-Object-Oriented-Prog 11
Create a Subclassclass SmartTurtle(Turtle): def drawSquare(self): for i in range (0 ,4): self.turnRight () self.forward ()
>>> earth1 = World()>>> smarty = SmartTurtle(earth1)>>> smarty.drawSquare()
![Page 12: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/12.jpg)
06-Intro-Object-Oriented-Prog 12
Passing Parameters to Methodsclass SmartTurtle(Turtle):
def drawSquare(self): for i in range (0 ,4): self.turnRight () self.forward ()
def drawSquare(self, width): for i in range (0 ,4): self.turnRight () self.forward(width)
>>> mars = World ()>>> tina = SmartTurtle(mars)>>> tina.drawSquare (30)>>> tina.drawSquare (150)>>> tina.drawSquare (100)
![Page 13: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/13.jpg)
06-Intro-Object-Oriented-Prog 13
Creating the Slide Classclass Slide: def show(self): show(self.picture) blockingPlay(self.sound)
>>> slide1=Slide()>>> slide1.picture =
makePicture(getMediaPath("barbara.jpg"))>>> slide1.sound = makeSound(getMediaPath("bassoon-c4.wav"))>>> slide1.show()
![Page 14: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/14.jpg)
06-Intro-Object-Oriented-Prog 14
Constructors
class slide:def __init__(self , pictureFile ,soundFile ):self.picture = makePicture(pictureFile)self.sound = makeSound(soundFile)
def show(self ):show(self.picture)blockingPlay(self.sound)
![Page 15: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/15.jpg)
06-Intro-Object-Oriented-Prog 15
Testing the constructordef playSlideShow2 ():
pictF = getMediaPath("barbara.jpg")soundF = getMediaPath("bassoon -c4.wav")slide1 = slide(pictF ,soundF)pictF = getMediaPath("beach.jpg")soundF = getMediaPath("bassoon -e4.wav")slide2 = slide(pictF ,soundF)pictF = getMediaPath("church.jpg")soundF = getMediaPath("bassoon -g4.wav")slide3 = slide(pictF ,soundF)pictF = getMediaPath("jungle2.jpg")soundF = getMediaPath("bassoon -c4.wav")slide4 = slide(pictF ,soundF)slide1.show ()slide2.show ()slide3.show ()slide4.show ()
![Page 16: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/16.jpg)
06-Intro-Object-Oriented-Prog 16
Object-oriented with Pictures>>> pic=Picture(getMediaPath("barbara.jpg"))>>> pic.show()>>> pic.getWidth()>>> pic.getHeight()>>> pixels = pic.getPixels()>>> pixel = pic.getPixel(0,0)>>> color = pixel.getColor();>>> red = pixel.getRed()
![Page 17: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/17.jpg)
06-Intro-Object-Oriented-Prog 17
Picture Functionsdef show(picture ):
if not picture.__class__ == Picture:print "show(picture ): Input is not a picture"raise ValueError
picture.show ()
![Page 18: 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python](https://reader036.vdocument.in/reader036/viewer/2022082602/5a4d1b8c7f8b9ab0599bf447/html5/thumbnails/18.jpg)
06-Intro-Object-Oriented-Prog 18
Summary• Procedural programming has a focus on the
procedures (tasks)• Object-oriented programming has a focus on the
objects and on simulation• You can create objects from classes in Python
– Picture(file) or Class(parameters)• You can create subclasses in Python
– class SmartTurtle(Turtle):• You can overload methods by passing different
parameters