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.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Object-Oriented Programming.
Chapter 4 Objects and Graphics
Python: Graphics
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
Animation CSC 161: The Art of Programming Prof. Henry Kautz 10/14/2009.
CSC 110 Objects and Graphics [Reading: chapter 4] CSC 110 E 1.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 Graphics.  We’ve been doing command line programming, but now it’s time to try GUI programming—programming in a graphical user interface, using.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Some Graphics CS303E: Elements of Computers and Programming.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Python Programming, 1/e1 Programming Thinking and Method (5a) Zhao Hai 赵海 Department of Computer Science and Engineering Shanghai Jiao Tong University.
Lecture 04 – Models of memory Mutable and immutable data.
Xiaojuan Cai Computational Thinking 1 Lecture 5 Objects and Graphics Xiaojuan Cai (蔡小娟) Fall, 2015.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
LING 408/508: Programming for Linguists Lecture 23 November 25 th.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics.
5. COMPUTING WITH GRAPHICS OBJECTS Dennis Y. W. Liu and Rocky K. C. Chang October 8, 2015 (Adapted from John Zelle’s slides)
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
CS 115 Lecture 7 Graphics – coordinate systems, Text, Entry, aliases Taken from notes by Dr. Neil Moore.
Vahé Karamian Python Programming CS-110 CHAPTER 4 Objects and Graphics.
CS 115 Lecture 6 Graphics Taken from notes by Dr. Neil Moore.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Killer cars.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
GUI in Python Ismail Abumuhfouz.
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
Python Programming: An Introduction to Computer Science
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Python: Simple Graphics and Event-driven Programming
Graphics Part I Taken from notes by Dr. Neil Moore
Graphics Part I Taken from notes by Dr. Neil Moore
Teach A-Level Computer Science: Object-Oriented Programming in Python
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
4. sequence data type Rocky K. C. Chang 16 September 2018
Graphics Part I Taken from notes by Dr. Neil Moore
5. Functions Rocky K. C. Chang 30 October 2018
Simple Graphics Package
References.
Topics Graphical User Interfaces Using the tkinter Module
Reference semantics, variables and names
Which best describes the relationship between classes and objects?
Class 7 coordinates: pixel and with setCoords aspect ratio Rectangle
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Objects Management.
Creating and Using Classes
Presentation transcript:

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)

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.

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

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:

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 Introduction to Computer Science Using Python. Wiley.

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 Introduction to Computer Science Using Python. Wiley.

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

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.

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

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 Introduction to Computer Science Using Python. Wiley.

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

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

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

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

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.

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] ?

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.

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): …

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.

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.

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()

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.

END