Lecture 17: Classes (Chapter 15)

Similar documents
Lecture 17: Classes (Chapter 15)

Announcements for This Lecture

Announcements for This Lecture

Lecture 18: Using Classes Effectively (Chapter 16)

Lecture 24: Loop Invariants

Lecture 3: Functions & Modules

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

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

CS 1110: Introduction to Computing Using Python Loop Invariants

Conditionals & Control Flow

Lecture 10: Lists and Sequences

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

Lecture 6: Specifications & Testing

Lecture 2: Variables & Assignments

CS 1110 Final, December 17th, Question Points Score Total: 100

Announcements for this Lecture

Lecture 7. Memory in Python

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

CS 1110 Prelim 2 Solutions April 2018

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

Lecture 9: Memory in Python

Lecture 11: Iteration and For-Loops

CS Lecture 19: Loop invariants

Review 2. Classes and Subclasses

Lecture 19: Subclasses & Inheritance (Chapter 18)

Lecture 4: Defining Functions

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

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

CSC148 Recipe for Designing Classes

CSC148: Week 1

Lecture 5: Strings

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 4. Defining Functions

Object-Oriented Python

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

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

CS 1110 Prelim 2 November 14th, 2013

Lecture 20: While Loops (Sections 7.3, 7.4)

CS 1110 Final, December 8th, Question Points Score Total: 100

Lecture 19. Operators and Abstraction

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

isinstance and While Loops

CS 1110 Final, December 16th, 2013

Lecture 21. Programming with Subclasses

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

PREPARING FOR THE FINAL EXAM

CS 1110 Final, May 2017

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 1110 Final Exam May 9th, 2013 SOLUTIONS

PREPARING FOR PRELIM 2

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

CS 1110 Prelim 1 October 4th, 2012

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Lecture 9. Memory and Call Stacks

PREPARING FOR PRELIM 1

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

CS 1110 Prelim 2 November 6th, 2012

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Lecture 10. Memory in Python

CS 1110, LAB 3: STRINGS; TESTING

Iteration and For Loops

Functions!!! Why functions? Functions provide good way to design systems!

Lecture 21. Programming with Subclasses

CS 1110 Final Exam, May 2018

Lecture 21. Programming with Subclasses

Python Development with PyDev and Eclipse -

CS Lecture 26: Grab Bag. Announcements

Chapter 2 Writing Simple Programs

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

CS 1110 Final, December 9th, Question Points Score Total: 100

Lecture 11. Asserts and Error Handling

CS 1110 Final Exam Solutions May 15th, 2014

CS 1110 Prelim 1 October 15th, 2015

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

CS 1110 Final, December 16th, 2013

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Lecture 4. while and for loops if else test Tuples Functions. Let us start Python Ssh (putty) to UNIX/Linux computer puccini.che.pitt.

61A Lecture 2. Friday, August 28, 2015

Final Exam Version A

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Lecture 18. Classes and Types

CS 1110 Final Solutions May 2017

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

Introduction to Python

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

CS1110. Lecture 6: Function calls

CS 1110 Prelim 1, March 2018

Advanced Python. Executive Summary, Session 1

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

Acct. balance Acct. balance 50 25

CS1110. Lecture 6: Function calls

CS Prelim 2 Review Fall 2014

CS Prelim 2 Review Fall 2017

Last Name: First Name: Cornell NetID, all caps: CS 1110 Regular Prelim 1 Solutions March 2018

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

A Problem. Loop-Body Returns. While-Loop Solution with a Loop-Body Return. 12. Logical Maneuvers. Typical While-Loop Solution 3/8/2016

Structure and Flow. CS 3270 Chapter 5

Lecture 4. Defining Functions

Transcription:

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 17: Classes (Chapter 15) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

Recall: Objects as Data in Folders nums = [2,3,5] nums[1] = 7 An object is like a manila folder Contains variables called attributes Can change attribute values (w/ assignment statements) Tab identifies it Unique number assigned by Python Fixed for lifetime of the object unique identifier Global Space nums Type listed in the corner 2 id1 Heap Space id1 0 2 1 3 2 5 type list 7

Classes are user-defined Types Classes are how we add new types to Python id2 class name Point3 Example Classes Point3 Card Rect Person x 2 y 3 z 5 3

class <class-name>(): """Class specification""" <method definitions> Simple Class Definition 4

The Class Specification class (): """Instance is a Cornell student Instance Attributes: netid: student's netid [str], 2-3 letters + 1-4 numbers courses: list of tuples (name [str], n [int]) name is course name, n is num credits major: declared major [str] """ 5

Constructors Function to create new instances function name is the class name Created for you automatically Calling the constructor: Makes a new object folder Initializes attributes (see next slide) Returns the id of the folder courses = [("CS 1110", 4), ("MATH 1920", 3)] s = ("abc1", courses, "Music") Global Space courses id8 netid courses major s id2 id8 'abc1' id2 "Music" folder not drawn Heap Space 6

two underscores Special Method: init def init (self, netid, courses, major): """Initializer: creates a Has netid, courses and a major called by the constructor Global Space courses id2 s id8 netid: [str], 2-3 letters + 1-4 numbers courses: list of tuples (name [str], n [int]) name is course name, n is number of credits major: declared major [str] self.netid = netid self.courses = courses self.major = major use self to assign attributes id8 netid courses major Heap Space 'abc1' id2 "Music" s = ("abc1", courses, "Music") # this is the call to the constructor, which calls init 7

Evaluating a Constructor Expression s = ("abc1", courses, "Music") 1. Creates a new object (folder) of the class on the heap Folder is initially empty 2. Executes the method init self = folder name = identifier Other arguments passed in order Executes commands in initializer 3. Returns folder name, the identifier Global Space courses id2 s id8 id8 Heap Space netid 'abc1' courses id2 major "Music" 8

Which statement is false? A) The constructor creates the folder B) A constructor calls the init method C) The constructor returns the id of the folder D) init puts attributes in the folder E) init returns the id of the folder 9

Invariants Properties of an attribute that must be true Works like a precondition: If invariant satisfied, object works properly If not satisfied, object is corrupted Examples: Point3 class: all attributes must be ints RGB class: all attributes must be ints in 0..255 Purpose of the class specification (see example on slide 5) 10

Checking Invariants with an Assert class (): """Instance is a Cornell student """ def init (self, netid, courses, major): """Initializer: instance with netid, and courses which defaults empty netid: [str], 2-3 letters + 1-4 numbers courses: list of tuples (name [str], n [int]) name is course name, n is number of credits major: declared major [str] """ assert type(netid) == str, "netid should be type str" assert netid[0].isalpha(), "netid should begin with a letter" assert netid[-1].isdigit(), "netid should end with an int" assert type(courses) == list, "courses should be a list" assert type(major) == str, "major should be type str" self.netid = netid self.courses = couress self.major = major 11

What if We want to track and limit the number of credits a student is taking. id5 id6 id7 netid 'abc1' courses id2 netid 'def456' courses id3 netid 'gh7890' courses id4 major "Music" major "History" major "CS" n_credit 15 max_credit n_credit 14 max_credit n_credit 21 max_credit Anything wrong with this? 12

Class Attributes Class Attributes: Variables that belong to the Class One variable for the whole Class Shared by all object instances Access by <Class Name>.<attribute-name> Why? Some variables are relevant to every object instance of a class Does not make sense to make them object attributes Doesn t make sense to make them global variables, either Example: we want limit the number of credits any student can take 13

Class Variables for CS1110 class (): """Instance is a Cornell student """ max_credit = def init (self, NetID, courses, major): # < specs go here > # < assertions go here > self.netid = netid self.courses = couress self.major = major self.n_credit = 0 for (course, n) in courses: self.n_credit = self.n_credit + n Where does max_credit live??? # add up all the credits assert self.n_credit <=.max_credit, "over credit limit" 14

Object Folders Separate for each instance Classes Have Folders Too Example: 2 objects id5 s1 s2 id5 id6 netid courses major 'abc1' id2 "Music" n_credit 15 id6 netid courses major 'def456' id3 "History" n_credit 14 Class Folders Data common to all instances max_credit Not just data! Everything common to all instances goes here! 15

Objects can have Methods Function: call with object as argument <function-name>(<arguments>) len(my_list) Method: function tied to the object <object-variable>.<function-call> my_list.count(7) Attributes live in object folder Class Attributes live in class folder Methods live in class folder id5 netid courses major max_credit 'abc1' id2 "Music" n_credit 15 init (self, netid, courses, major) 16

Complete Class Definition keyword class Beginning of a class definition Specification (similar to one for a function) to define class variables class <class-name>(): """Class specification""" <assignment statements> <method definitions> max_credit init (self, netid, courses, major) to define class methods class (): """Specification goes here."" max_credit = def init (self, netid, courses, major):... <snip>... Python creates after reading the class definition 17

Method Definitions Looks like a function def But indented inside class 1 st parameter always self Example: s1.enroll("aem 2400", 4) Go to class folder for s1 (i.e., ) that s where enroll is defined Now enroll is called with s1 as its first argument Now enroll knows which instance of it is working with class (): def init (self, netid, courses, major): self.netid = netid self.courses = courses self.major = major # < rest of constructor goes here > def enroll(self, name, n): if self.n_credit + n >.max_credit: print("sorry your schedule is full!") else: max_credit init (self, netid, courses, major) enroll(self, new_coures, n) self.courses.append((name, n)) self.n_credit = self.n_credit + n print("welcome to "+ name) 18

Class Gotchas and how to avoid them Rules to live by: 1. Refer to Class Attributes using the Class Name s1 = ( xy14, [], "History") print( max credits = +str(.max_credit)) 2. Don t forget self 19

Name Resolution for Objects object. name means Go the folder for object Find attribute/method name If missing, check class folder If not in either, raise error s1 id5 id5 netid 'abc1' courses id2 major "Music" n_credit 15 s1 = ( xy14, [], "History") # finds attribute in object folder print(s1.netid) # finds attribute in class folder max_credit init (self, netid, courses, major) enroll(self, new_coures, n) print(s1.max_credit) ß dangerous 20

Accessing vs. Modifying Class Variables Recall: you cannot assign to a global variable from inside a function call Similarly: you cannot assign to a class attribute from inside an object variable s1 = ( xy14, [], "History").max_credit = # updates class attribute s1.max_credit = 24 # creates new object attribute # called max_credit Better to refer to Class Variables using the Class Name 21

What gets Printed? (Q) import cs1110 s1 = cs1110.( jl200", [], "Art") print(s1.max_credit) s1 = cs1110.( jl202", [], "History") print(s2.max_credit) s2.max_credit = print(s1.max_credit) print(s2.max_credit) print(cs1110..max_credit) A: C: B: D:

What gets Printed? (A) import cs1110 s1 = cs1110.( jl200", [], "Art") print(s1.max_credit) s1 = cs1110.( jl202", [], "History") print(s2.max_credit) s2.max_credit = print(s1.max_credit) print(s2.max_credit) print(cs1110..max_credit) A: C: CORRECT B: D:

Class Gotchas and how to avoid them Rules to live by: 1. Refer to Class Attributes using the Class Name s1 = ( xy14, [], "History") print( max credits = +str(.max_credit)) 2. Don t forget self 24

Don t forget self, Part 1 s1 = ( xy14, [], "History") s1.enroll("aem 2400", 4) <var>.<method_name> always passes <var> as first argument TypeError: enroll() takes 2 positional arguments but 3 were given class (): def init (self, netid, courses, major): self.netid = netid self.courses = courses self.major = major # < rest of constructor goes here > def enroll(self, name, n): # if you forget self if self.n_credit + n >.max_credit: print("sorry your schedule is full!") else: self.courses.append((name, n)) self.n_credit = self.n_credit + n print("welcome to "+ name) 25

Don t forget self, Part 2 s1 = ( xy14, [], "History") s1.enroll("aem 2400", 4) class (): def init (self, netid, courses, major): self.netid = netid self.courses = courses self.major = major # < rest of constructor goes here > # if you forget self à NameError: global name n_credit' is not defined def enroll(self, name, n): if self.n_credit + n >.max_credit: print("sorry your schedule is full!") else: self.courses.append((name, n)) self.n_credit = self.n_credit + n print("welcome to "+ name) 26

Aside: The Value None The major field is a problem. major is a declared major Some students don't have one! Solution: use value None None: Lack of str Will reassign the field later! id5 netid 'abc1' courses id2 major None n_credit 15 27

Making Arguments Optional We can assign default values to init arguments Write as assignments to parameters in definition Parameters with default values are optional Examples: s1 = ( xy14, [], "History") # all parameters given s1 = ( xy14, courses) # netid, courses given, major defaults to None s1 = ( xy14, major="art") # netid, major given, courses defaults to [] class (): def init (self, netid, courses=[], major=none): self.netid = netid self.courses = courses self.major = major # < rest of constructor goes here > 28