Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

Similar documents
Announcements for this Lecture

Lecture 7. Memory in Python

Lecture 17: Classes (Chapter 15)

Lecture 2: Variables & Assignments

CS1110. Lecture 6: Function calls

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

CS 1110 Final, December 7th, 2017

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Conditionals & Control Flow

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 17: Classes (Chapter 15)

Lecture 5. Defining Functions

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

CS1110. Lecture 6: Function calls

Definition. Pointers. Outline. Why pointers? Definition. Memory Organization Overview. by Ziad Kobti. Definition. Pointers enable programmers to:

Lecture 6: Specifications & Testing

Lecture 4: Defining Functions

Lecture 10: Lists and Sequences

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

CS 1110, LAB 3: STRINGS; TESTING

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Sage Estimating. (formerly Sage Timberline Estimating) Getting Started Guide

Lecture 3: Functions & Modules

CS1 Lecture 3 Jan. 18, 2019

Sage Estimating (formerly Sage Timberline Estimating) Getting Started Guide. Version has been retired. This version of the software

Lecture 11: Iteration and For-Loops

Lecture 19: Subclasses & Inheritance (Chapter 18)

Lecture 24: Loop Invariants

Storage Allocation CSE 143. Pointers, Arrays, and Dynamic Storage Allocation. Pointer Variables. Pointers: Review. Pointers and Types

Lecture 9. Memory and Call Stacks

521493S Computer Graphics Exercise 3 (Chapters 6-8)

Lecture 1. Types, Expressions, & Variables

CS 1110: Introduction to Computing Using Python Loop Invariants

Announcements for This Lecture

CS1110 Lab 1 (Jan 27-28, 2015)

CS1 Lecture 3 Jan. 22, 2018

Lecture 18: Using Classes Effectively (Chapter 16)

This version of the software

Lecture 10. Memory in Python

has been retired This version of the software Sage Timberline Office Get Started Document Management 9.8 NOTICE

Lecture 9: Memory in Python

Sage Estimating (SQL) (formerly Sage Timberline Estimating) Installation and Administration Guide. Version 16.11

Lecture 20: While Loops (Sections 7.3, 7.4)

Who. Winter Compiler Construction Generic compiler structure. Mailing list and forum. IC compiler. How

Lecture 1. Course Overview Types & Expressions

Assignment 7: Due Wednesday May 11 at 6pm UPDATES on Monday May 9

Simple example. Analysis of programs with pointers. Points-to relation. Program model. Points-to graph. Ordering on points-to relation

Lecture06: Pointers 4/1/2013

Truth Trees. Truth Tree Fundamentals

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

Readings for This Lecture

CS 1110 SPRING 2016: LAB 3: PRACTICE FOR A2 (Feb 23-24)

Course Overview, Python Basics

Lecture 8: Orthogonal Range Searching

Lecture 3: Geometric Algorithms(Convex sets, Divide & Conquer Algo.)

Iteration and For Loops

Lecture 12. Lists (& Sequences)

Assignment 4: Due Friday Mar 11 at 6pm

An Efficient Coding Method for Coding Region-of-Interest Locations in AVS2

Real Numbers and Algebraic Expressions

CS 1110, LAB 1: PYTHON EXPRESSIONS.

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Announcements for This Lecture

Assignment 3: Due Friday Mar 4 at 6pm on CMS Updates in orange/red; last update 12:10pm Sun Feb 28

CIS192: Python Programming

Lecture 1. Course Overview, Python Basics

CENTRAL AND PARALLEL PROJECTIONS OF REGULAR SURFACES: GEOMETRIC CONSTRUCTIONS USING 3D MODELING SOFTWARE

Equality-Based Translation Validator for LLVM

CS Lecture 19: Loop invariants

1.5 Case Study. dynamic connectivity quick find quick union improvements applications

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Lecture 5: Strings

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

Intro. Scheme Basics. scm> 5 5. scm>

Sage Document Management Version 17.1

Lecture 18. Today, we will discuss developing algorithms for a basic model for parallel computing the Parallel Random Access Machine (PRAM) model.

Announcements for the Class

CMSC 425: Lecture 16 Motion Planning: Basic Concepts

Lecture 2. Variables & Assignment

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

A Study of Protocols for Low-Latency Video Transport over the Internet

Lecture 1. Course Overview, Python Basics

CS 1110, LAB 03: STRINGS; TESTING First Name: Last Name: NetID:

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

A Brief Introduction to Truth-Table Logic. Kent Slinker Pima Community College

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

Optimizing Dynamic Memory Management!

A DEA-bases Approach for Multi-objective Design of Attribute Acceptance Sampling Plans

Verification Options. To Store Or Not To Store? Inside the UPPAAL tool. Inactive (passive) Clock Reduction. Global Reduction

Programming for Engineers in Python. Autumn

Improved heuristics for the single machine scheduling problem with linear early and quadratic tardy penalties

SEARCH ENGINE MANAGEMENT

isinstance and While Loops

10 File System Mass Storage Structure Mass Storage Systems Mass Storage Structure Mass Storage Structure FILE SYSTEM 1

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Lecture 2: Fixed-Radius Near Neighbors and Geometric Basics

CS 428: Fall Introduction to. Geometric Transformations. Andrew Nealen, Rutgers, /15/2010 1

Source Coding and express these numbers in a binary system using M log

Lecture 24. GUI Applications

Transcription:

htt://www.cs.cornell.edu/courses/cs1110/2018s Lecture 7: Objects (Chater 15) CS 1110 Introduction to Comuting Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

Good news about Assignment A1 & Lab 3 Now: Otional one-on-one rogramming ractice with a staff member to hel just you, Sign u on CMS under the Otional 1-on-1 assignment This week: you have two weeks to do lab 3, and it hels you with A1. Break: There will be some consulting hours. Check the calendar Next week: no new lab. All Wed. Feb 21 labs are dro-in office hours oen to all. (Nobody at the Tuesday labs break). 2

3

Partnering (See section 1.1) You may do this assignment with at most one other erson Make artnershi on official on CMS before either of you submit any files If your artnershi dissolves, there are secial grou divorce rocedures you must follow Take this very seriously! 4

Academic Integrity Rules Gloss (1.2) Never look at another else s code. Never show your code or talk about lines of your actual code to someone else (excet course staff). DO secifically acknowledge by name all hel you received, whether or not it was legal 5

Submit early and often Submit your first draft to CMS by Monday, February 26 at 2m. You can relace older submissions with imroved ones u to the deadline of 11:59m. Benefits: ractice with CMS before deadline is looming alert us during business hours if roblems arise You have been warned to submit by 2m. Do not exect that we will accet work that doesn t make it onto CMS on time, for whatever reason. 2/14/17 6

Tye: set of values & oerations on them Tye float: Values: real numbers Os: +, -, *, /, ** Tye int: Values: integers Os: +, -, *, //, %, ** Tye bool: Values: integers Os: not, and, or Tye str: Values: string literals Double quotes: abc Single quotes: abc Os: + (concatenation) 7

Built-in Tyes are not Enough Want a oint in 3D sace We need three variables x, y, z coordinates What if have a lot of oints? Vars x0, y0, z0 for first oint Vars x1, y1, z1 for next oint This can get really messy How about a single variable that reresents a oint? x 2 y 3 z 5 8

Built-in Tyes are not Enough Want a oint in 3D sace We need three variables x, y, z coordinates What if have a lot of oints? Vars x0, y0, z0 for first oint Vars x1, y1, z1 for next oint This can get really messy How about a single variable that reresents a oint? Can we stick them together in a folder? Motivation for objects x 2 y 3 z 5 9

Objects: Organizing Data in Folders An object is like a manila folder It contains other variables Variables are called attributes These values can change It has an ID that identifies it Unique number assigned by Python (just like a NetID for a Cornellian) Cannot ever change Has no meaning; only identifies Unique tab identifier x 2 y 3 z 5 10

Classes: user-defined tyes Values must have a tye An object is a value Object tye is a class Modules rovide classes Examle: shaes.y Defines:, Rectangle classes x 2 y 3 z 5 class name 11

Constructor: Function to make Objects How do we create objects? Other tyes have literals No such thing for objects Constructor Function: Format: class name ( arguments ) Examle: (0,0,0) Makes a new object (manila folder) with a new id Called an instantiated object Returns folder id as value Examle: = (0, 0, 0) Creates a Point object x 0 y 0 z 0 Variable stores ID not object instantiated object Stores object s id in 12

Constructors and Modules >>> imort shaes >>> = shaes.(0,0,0) >>> id() Need to imort module that has Point class. Constructor is function. Prefix w/ module name. Shows the id of x 0 y 0 z 0 13

Accessing Attributes Attributes are variables that live inside of objects Can use in exressions Can assign values to them Format: variable. attribute Examle:.x Look like module variables To evaluate.x, Python: 1. finds folder with id stored in 2. returns the value of x in that folder id3 id3 x 1 y 2 z 3 14

Accessing Attributes Examle Examle: = shaes.(1, 2, 3).x =.x + 3 id3 id3 x 1 x 4 y 2 z 3 15

Object Variables Variable stores object id Reference to the object 1 2 Reason for folder analogy Assignment uses object id Examle: 1 = shaes.(0, 0, 0) 2 = 1 Takes contents from 1 Puts contents in 2 Does not make new folder! x 0 y 0 z 0 This is the cause of many mistakes in this course 16

Attribute Assignment (Question) >>> = shaes.(0,0,0) >>> q = Execute the assignments: >>>.x = 5 >>> q.x = 7 id4 id4 q id4 What is value of.x? x 0 A: 5 B: 7 C: id4 D: I don t know y 0 z 0 17

Attribute Assignment (Solution) >>> = shaes.(0,0,0) >>> q = Execute the assignments: >>>.x = 5 >>> q.x = 7 What is value of.x? A: 5 B: 7 CORRECT C: id4 D: I don t know id4 q id4 id4 x 0x 5 7 y 0 z 0 x 18

Call Frames and Objects (1) Objects can be altered in a function call Object variables hold ids! Folder can be accessed from global variable or arameter Examle: 1 def incr_x(q): q.x = q.x + 1 >>> = shaes.(1, 2, 3) >>> incr_x() Global Memory id5 id5 x 1 Call Frame incr_x 1 q id5 20

Call Frames and Objects (2) Objects can be altered in a function call Object variables hold ids! Folder can be accessed from global variable or arameter Examle: 1 def incr_x(q): q.x = q.x + 1 >>> = shaes.(1, 2, 3) >>> incr_x() Global Memory id5 id5 x x 1 2 Call Frame incr_x q id5 RETURN NONE 21

Call Frames and Objects (3) Objects can be altered in a function call Object variables hold ids! Folder can be accessed from global variable or arameter Examle: 1 def incr_x(q): q.x = q.x + 1 Global Memory id5 id5 x x 1 2 Call Frame >>> = shaes.(1, 2, 3) >>> incr_x() 22

How Many Folders (Question) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) Draw everything that gets created. How many folders get drawn? 23

How Many Folders (Solution) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) Draw everything that gets created. How many folders get drawn? 2/16/17 x 1 y 2 z 3 x 3 y 4 z 5 24

What Else? (Question) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) Draw everything that gets created. How many folders get drawn? What else gets drawn? 2/16/17 x 1 y 2 z 3 x 3 y 4 z 5 25

What Else? (Solution) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) Draw everything that gets created. How many folders get drawn? What else gets drawn? q 2/16/17 x 1 y 2 z 3 x 3 y 4 z 5 26

Swa (Question) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) Execute swa_x on what we just drew. There should be a call frame. What is in.x at the end? def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t A: 1 B: 2 C: 3 D: I don t know 27

Swa (Solution) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) Execute swa_x on what we just drew. There should be a call frame. What is in.x at the end? def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t A: 1 B: 2 C: 3 CORRECT D: I don t know 28

Swa Exlanation (1) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t q x 1 y 2 x 3 y 4 swa_x:1 1 q t z 3 z 5 29

Swa Exlanation (2) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t q x 1 y 2 x 3 y 4 swa_x:1 2 q t 1 z 3 z 5 30

Swa Exlanation (3) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t q x x 1 3 y 2 x 3 y 4 swa_x:1 3 q t 1 z 3 z 5 31

Swa Exlanation (4) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t q x x 1 3 y 2 z 3 x x 3 1 y 4 z 5 swa_x:1 q t 1 Return NONE 32

Swa Exlanation (5) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa_x(, q) def swa_x(, q): 1 t =.x 2.x = q.x 3 q.x = t q ERASE WHOLE FRAME x x 1 3 x x 3 1 y 2 y 4 z 3 z 5 33

Global (Question) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) Before calling swa(, q): q def swa(, q): 1 t = 2 = q 3 q = t What is in global after calling swa? A: B: C: I don t know 34

Global (Solution) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) After calling swa(, q): q def swa(, q): 1 t = 2 = q 3 q = t What is in global after calling swa? A: CORRECT B: C: I don t know 35

Global Exlanation (1) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) def swa(, q): 1 t = 2 = q 3 q = t q x 1 Point x 3 Point swa 1 q y 2 y 4 z 3 z 5 36

Global Exlanation (2) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) def swa(, q): 1 t = 2 = q 3 q = t q x 1 y 2 Point x 3 y 4 Point swa 2 q t z 3 z 5 37

Global Exlanation (3) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) def swa(, q): 1 t = 2 = q 3 q = t q x 1 y 2 Point x 3 y 4 Point swa t x q 3 z 3 z 5 38

Global Exlanation (4) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) def swa(, q): 1 t = 2 = q 3 q = t q x 1 y 2 z 3 Point x 3 y 4 z 5 Point swa t x RETURN q x NONE 39

Global Exlanation (5) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) def swa(, q): 1 t = 2 = q 3 q = t q Point Point ERASE WHOLE FRAME x 1 x 3 y 2 y 4 z 3 z 5 40

Global Revisited (Question) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) Before calling swa(, q): q def swa(a, b): 1 t = a 2 a = b 3 b = t What is in global after calling swa? A: B: C: I don t know 41

Global Revisited (Solution) imort shaes = shaes.(1,2,3) q = shaes.(3,4,5) swa(, q) After calling swa(, q): Same question as the revious one, (excet we re-named the arameters) but this time it seems obvious that global is unaffected. q def swa(a, b): 1 t = a 2 a = b 3 b = t What is in global after calling swa? A: CORRECT B: C: I don t know 42

Methods: Functions Tied to Classes Method: function tied to object Method call looks like a function call receded by a variable name: id3 Examle: variable. method ( arguments ) imort shaes = shaes.(1,2,3).greet() Hi! I am a 3-dimensional oint located at (4,2,3) id3 x 5 y 2 z 3 43

Built-in Tyes vs. Classes Built-in tyes Built-into Python Refer to instances as values Instantiate with literals Can ignore the folders Classes Provided by modules Refer to instances as objects Instantiate w/ constructors Must reresent with folders 44

Where To From Here? First, Understand objects All Python rograms use objects Most small rograms use objects of classes that are art of the Python Library Eventually, create your own classes: the heart of OO Programming the rimary tool for organizing Python rograms But we need to learn more basics first! 45