Presentation is loading. Please wait.

Presentation is loading. Please wait.

06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python.

Similar presentations


Presentation on theme: "06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python."— Presentation transcript:

1 06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python

2 06-Intro-Object-Oriented-Prog2 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

3 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 names

4 06-Intro-Object-Oriented-Prog4 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

5 06-Intro-Object-Oriented-Prog5 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

6 06-Intro-Object-Oriented-Prog6 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

7 06-Intro-Object-Oriented-Prog7 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

8 06-Intro-Object-Oriented-Prog8 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()

9 06-Intro-Object-Oriented-Prog9 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

10 06-Intro-Object-Oriented-Prog10 Challenge Use a turtle to draw a square Use a turtle to draw blocks of color using a wide pen trail

11 06-Intro-Object-Oriented-Prog11 Create a Subclass class SmartTurtle(Turtle): def drawSquare(self): for i in range (0,4): self.turnRight () self.forward () >>> earth1 = World() >>> smarty = SmartTurtle(earth1) >>> smarty.drawSquare()

12 06-Intro-Object-Oriented-Prog12 Passing Parameters to Methods class 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)

13 06-Intro-Object-Oriented-Prog13 Creating the Slide Class class 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()

14 06-Intro-Object-Oriented-Prog14 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)

15 06-Intro-Object-Oriented-Prog15 Testing the constructor def 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 ()

16 06-Intro-Object-Oriented-Prog16 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()

17 06-Intro-Object-Oriented-Prog17 Picture Functions def show(picture ): if not picture.__class__ == Picture: print "show(picture ): Input is not a picture" raise ValueError picture.show ()

18 06-Intro-Object-Oriented-Prog18 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


Download ppt "06-Intro-Object-Oriented-Prog1 Barb Ericson Georgia Institute of Technology July 2009 Introduction to Object-Oriented Programming in Python."

Similar presentations


Ads by Google