Presentation is loading. Please wait.

Presentation is loading. Please wait.

10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John.

Similar presentations


Presentation on theme: "10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John."— Presentation transcript:

1 10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John Zelle's slides)

2 Objectives Explain the difference between a reference and dereferenced value. Describe the use of object references. Explain the concept of memory allocation and deallocation. Effectively use objects in Python. Explain the relationship between class and objects.

3 Objects in Python All values in Python are represented as objects, including lists, as well as numeric values. Rocky Chang Methods split() lower() upper() capitalize() Data … A string object student Your program

4 EXERCISE 10.1 Try list1 = ["CBS", "NBC", "ABC"] list2 = [4, 1, 2, 3] list3 = ["CBS", "NBC", "ABC", 4, 1, 2, 3] sorted(list1); sorted(list2); sorted(list3) list1.sort(); list2.sort(); list3.sort() More on sorting: https://docs.python.org/3/howto/sorting.htm

5 Procedural vs object-oriented An object contains a set of attributes, stored in a set of instance variables, and a set of functions called methods that provide its behavior. sort() is a function. sort() is a method for a list object. Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

6 Object references In Python, objects are represented as a reference to an object in memory. A reference is a value that references, or “points to,” the location of another entity. The value that a reference points to is called the dereferenced value. Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

7 EXERCISE 10.2 Try x, y = 10, 20; id(x); id(y) x = y; id(x); id(y) x, y = 30, 30; id(x); id(y) x == y x is y

8 Immutable objects An immutable object is an object whose state cannot be modified after it is created. So far, numbers, booleans, strings are immutable. The main advantage of immutable object is thread-safe, i.e., data cannot be altered in a multi-threaded program.

9 EXERCISE 10.3 Try list1 = [1,2,3,4,5] list2 = list1 id(list1) is id(list2) list1[0] = 10 list2[0] = ?

10 List copying and assignment List is a mutable object. List assignment assigns the reference of a list on the right of = to the variable on the left of =. Source: Charles Dierbach. 2013. Introduction to Computer Science Using Python. Wiley.

11 EXERCISE 10.4 Try list1 = [1,2,3,4,5] list2 = list(list1) id(list1) is id(list2) list1[0] = 10 list2[0] = ?

12 EXERCISE 10.5 Try list1 = [[1,2], [3,4], [5,6]] list1[0] ? list1[0][0] ? list1[0][1] ? list1[0][2] ?

13 EXERCISE 10.6 Try list2 = list1 list1 is list2 list1[0] = [10,20] list2[0] ?

14 EXERCISE 10.7 Try list2 = list(list1) list1 is list2 list1[0,0] = 100 list2[0,0] ? list1[0] = [100,200] list2[0] ?

15 Shallow copy The list constructor list() makes a copy of the top level of a list, in which the sublist (lower-level) structures are shared is referred to as a shallow copy. A deep copy operation makes a complete copy of a list. A deep copy operator is provided by method deepcopy of the copy module in Python.

16 EXERCISE 10.8 Try import copy list2 = copy.deepcopy(list1) list1 is list2 list1[0,0] = 100 list2[0,0] ? list1[0] = [100,200] list2[0] ?

17 Revisiting the graphics package win = graphics.GraphWin() GraphWin() is a method for creating (or instantiating) a GraphWin object, and win references to the object. win.close() Remove the object referenced to by win. Other objects in graphics.py: Point, Circle, Oval, Line, Text Rectangle and others. Find graphics.py from your system and check out the methods available for these objects.

18 Classes and objects class GraphWin(tk.Canvas): … class Transform: … class GraphicsObject: … class Point(GraphicsObject): … class _BBox(GraphicsObject): … class Rectangle(_BBox): … class Oval(_BBox): … class Circle(Oval): … class Line(_BBox): … class Polygon(GraphicsObject): … class Text(GraphicsObject): … class Entry(GraphicsObject): … class Image(GraphicsObject): …

19 A single class but infinitely many objects Each object is an instance of some class, and the class describes the properties of the instance. To create a new instance of a class, we use a special operation called a constructor. (,, …) is the name of the class we want to create a new instance of, e.g. Circle or Point. The parameters are required to initialize the object. For example, Point requires two numeric values. p = Point(50, 60) These values are stored as instance variables inside of the object.

20

21 Cloning objects and others Graphical objects have a clone method that will make a copy of the object. leftEye = Circle(Point(80, 50), 5) leftEye.setFill('yellow') leftEye.setOutline('red') rightEye = leftEye.clone() Event-driven programming draws interface elements (widgets) on the screen and then waits for the user to do something. An event is an object that encapsulates information about what just happened. The event object is sent to the appropriate part of the program to be processed, for example, a button event.

22 An event-driven program from graphics import * def main(): win = GraphWin() shape = Circle(Point(50,50), 20) shape.setOutline("red") shape.setFill("red") shape.draw(win) for i in range(10): p = win.getMouse() c = shape.getCenter() dx = p.getX() - c.getX() dy = p.getY() - c.getY() shape.move(dx, dy) win.close() main()

23 EXERCISE 10.9 What does the program on the last page do? Modify the program so that the program will terminate only when the new mouse click is within the current circle.

24 END


Download ppt "10. MORE OBJECTS Rocky K. C. Chang October 18, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and adapted from John."

Similar presentations


Ads by Google