Lecture 18: Using Classes Effectively (Chapter 16)

Similar documents
Lecture 17: Classes (Chapter 15)

Lecture 18. Methods and Operations

Lecture 19. Operators and Abstraction

Lecture 20: While Loops (Sections 7.3, 7.4)

Lecture 17: Classes (Chapter 15)

Lecture 21. Programming with Subclasses

Lecture 19. Using Classes Effectively

Lecture 18. Classes and Types

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses

Lecture 19: Subclasses & Inheritance (Chapter 18)

Review 2. Classes and Subclasses

CS 1110: Introduction to Computing Using Python Loop Invariants

Lecture 24: Loop Invariants

isinstance and While Loops

CS Lecture 19: Loop invariants

Lecture 2: Variables & Assignments

Announcements for This Lecture

Conditionals & Control Flow

Lecture 18. Using Classes Effectively

Announcements for This Lecture

CSC148 Recipe for Designing Classes

Lecture 10: Lists and Sequences

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

Lecture 3: Functions & Modules

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

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Iteration and For Loops

Lecture 4: Defining Functions

CS 1110: Introduction to Computing Using Python Lists and Sequences

PREPARING FOR PRELIM 2

Lecture 5: Strings

Lecture 11: Iteration and For-Loops

CME 193: Introduction to Scientific Python Lecture 4: File I/O and Classes

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

Lecture 6: Specifications & Testing

CS 1110, LAB 3: STRINGS; TESTING

CS1110. Lecture 6: Function calls

COMPSCI 105 S Principles of Computer Science. Classes 3

Lecture 20. Subclasses & Inheritance

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

CMSC201 Computer Science I for Majors

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

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

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

CSC148: Week 1

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

Announcements for this Lecture

PREPARING FOR THE FINAL EXAM

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

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

CS108 Lecture 16: User Defined Classes. Overview/Questions

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

OBJECT ORIENTED PROGRAMMING

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Python Development with PyDev and Eclipse -

CS 1110 Prelim 2 November 14th, 2013

CS 1110 Prelim 1 October 4th, 2012

PREPARING FOR PRELIM 1

CSC148 Intro. to Computer Science

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CS 1110 Final, May 2017

CS Prelim 2 Review Fall 2018

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

CS Prelim 1 Review Fall 2018

Abstract Data Types Chapter 1

20. More Complicated Classes. A Class For Manipulating Fractions. A Class For Manipulating Fractions 4/19/2016. Let s Define a Class to Do This Stuff

CS1110. Lecture 6: Function calls

Lecture 9: Memory in Python

Sorting and Searching

CMPSCI 187 / Spring 2015 Hangman

Lecture 11. Asserts and Error Handling

Lecture 11. Asserts and Error Handling

CS Prelim 2 Review Fall 2017

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

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

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

Classes, part Deux. Three Groups. CSE 231, Rich Enbody. Users Programmers Class Designers 11/11/13. Michigan State University CSE 231, Fall 2013

Lecture 4. Defining Functions

Creating a new data type

CSI33 Data Structures

Writing a Fraction Class

Course Overview, Python Basics

CS 1110 Prelim 2 November 6th, 2012

PREPARING FOR THE FINAL EXAM

CS 1110 Prelim 1 October 17th, 2013

CS Prelim 1 Review Fall 2017

CS Prelim 1 Review Fall 2013

CS Prelim 2 Review Fall 2014

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

CS 1110 Prelim 1 October 15th, 2015

CS Prelim 2 Review Fall 2012

Exceptions. raise type(message) raise Exception(message)

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CS1110 Lab 6 (Mar 17-18, 2015)

Programming I. Course 9 Introduction to programming

CS 1110 Prelim 2 Solutions April 2018

Lecture 38: Python. CS 51G Spring 2018 Kim Bruce

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

Transcription:

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

Announcements Website colors have changed. Don t be alarmed. A3 due Friday at 11:59pm. Spring break next week: Currently no office/consulting hours Keep an eye on the calendar Limited piazza 2

Method Definitions Looks like a function def But indented inside class 1 st parameter always self Example: p1.greet() Go to class folder for p1 (i.e., Student) that s where greet is defined Now greet is called with p1 as its first argument This way, greet knows which instance of Student it is working with id1 name NetID is_auditing Student Jon Li jl200 True class Student(): def init (self, name, NetID, is_auditing): self.name = name self.netid = NetID self.is_auditing = is_auditing Student.enrollment = Student.enrollment + 1 def greet(self): enrollment init (self, ) greet(self) """Prints information about the Student to the screen""" print("hi! My name is "+ self.name) print( My NetID is "+ self.netid) if self.is_auditing: print("i'm auditing the class") Student 0 3

We know how to make: Class definitions Class specifications The init function Attributes (using self) Class attributes Class methods 4

Special Methods in Python Start/end with 2 underscores This is standard in Python Used in all special methods Also for special attributes init for initializer str for str() repr for repr() eq for ==, lt for <, For a complete list, see class Point3(): """Instances are points in 3D space""" def init (self,x=0,y=0,z=0): """Initializer: makes new Point3""" https://docs.python.org/3/refere """Returns: unambiguous string""" nce/datamodel.html#basiccustomization return str(self. class ) + str(self) See Fractions example at the end of this presentation def str (self): """Returns: string with contents""" return '( +str(self.x) + ', + str(self.y) + ', + str(self.z) + ')' def repr (self): 5

Designing Types Type: set of values and the operations on them int: (set: integers; ops: +,, *, /, ) Point3 (set: x,y,z coordinates; ops: distanceto, ) Card (set: suit * rank combinations; ops: ==,!=, < ) New ones to think about: Person, Worker, Image, Date, etc. To define a class, think of a type you want to make 6

Making a Class into a Type 1. What values do you want in the set? What are the attributes? What values can they have? Are these attributes shared between instances (class attributes) or different for each attribute (instance attributes)? What are the class invariants: things you promise to keep true after every method call 2. What operations do you want? This often influences the previous question What are the method specifications: states what the method does & what it expects (preconditions) Are there any special methods that you will need to provide? Write your code to make it so! 7

Let s make the game Hangman There is a secret word. The user has 10 chances to guess letters until the word has been spelled out. Would be great to have a class SecretWord that would keep track of both the word we re guessing and what the user sees / has guessed so far. Play the game. 8

How is this going to be used? import random, hangman word_list = [ words we want user to guess.. ] N_GUESSES = 10 Set the secret word for n in list(range(n_guesses)): print the word so far user_guess = input("guess a letter: ") Apply guess to the secret word if check-if-solved: print("you WIN!!!") break #jumps out of the for-loop, not allowed for A3! Reveal the word 9

What should the SecretWord offer me? Like a string, but two of them: the secret word what the user sees I should be able to: Set the secret word Print out the word as guessed so far Determine whether the game is over Reveal the secret word 10

Example: SecretWord 1. What values do you want in the set? What are the attributes? What values can they have? Are these attributes shared between instances (class attributes) or different for each attribute (instance attributes)? What are the class invariants: things you promise to keep true after every method call 2. What operations do you want? This often influences the previous question What are the method specifications: states what the method does & what it expects (preconditions) Are there any special methods that you will need to provide? 11

Planning out Class: the Attributes class SecretWord(object): """A word to be guessed by a user in a game of hangman. Instance Attributes: secret_word: word being guessed [str of lower case letters] display_word: word as the user sees it: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length """ What are the attributes? What values can they have? Are these attributes shared between instances (class attributes) or different for each attribute (instance attributes)? What are the class invariants: things you promise to keep true after every method call 12

Planning out Class: the Methods def init (self, word): """Initializer: creates both secret_word and display_word from word [a str of lower case letters]""" def str (self): """Returns: both words"" def len (self): """Returns: the length of the secret word""" Are there any special methods that you will need to provide? What are their preconditions? You don t have to do this. But you should consider it. Careful. Make sure overloading is the right thing to do. 13

Planning out Class: the Methods def word_so_far(self): "" Prints the word being guessed"" def reveal(self): "" Prints the word being guessed"" def apply_guess(self, letter): """Updates the display_word to reveal all instances of letter as they appear in the secret_word. ( _ is replaced with letter) letter: the user's guess [1 character string A..Z] "" def is_solved(self): """Returns True if the entire word has been guessed""" What are the method specifications: states what the method does & what it expects (preconditions) 14

How is this going to be used? import random, hangman word_list = [ words we want user to guess.. ] N_GUESSES = 10 secret = hangman.secretword(random.choice(word_list)) for n in list(range(n_guesses)): secret.word_so_far() user_guess = input("guess a letter: ") secret.apply_guess(user_guess): if secret.is_solved(): print("you WIN!!!") break #jumps out of the for-loop, not allowed for A3! secret.reveal() 15

Implementing a Class All that remains is to fill in the methods. (All?!) When implementing methods: 1. Assume preconditions are true (checking is friendly) 2. Assume class invariant is true to start 3. Ensure method specification is fulfilled 4. Ensure class invariant is true when done Later, when using the class: When calling methods, ensure preconditions are true If attributes are altered, ensure class invariant is true 16

Implementing an Initializer def init (self, word): """Initializer: creates both secret_word and display_word from word [a str of lower case letters] "" # JOB OF THIS METHOD Instance variables: # WHAT BETTER BE TRUE WHEN WE RE DONE secret_word: [str of lower case letters] display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length 17

Implementing an Initializer (Q) def init (self, word): """Initializer: creates both secret_word and display_word from word [a str of lower case letters] "" # JOB OF THIS METHOD A SecretWord.secret_word = word SecretWord.display_word = len(word)*'_' B C secret_word = word display_word = len(word)*'_' self.secret_word = word self.display_word = len(word)*'_' Instance variables: # WHAT BETTER BE TRUE WHEN WE RE DONE secret_word: [str of lower case letters] display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length 18

Implementing an Initializer (A) def init (self, word): """Initializer: creates both secret_word and display_word from word [a str of lower case letters] "" # JOB OF THIS METHOD A SecretWord.secret_word = word SecretWord.display_word = len(word)*'_' B C secret_word = word display_word = len(word)*'_' self.secret_word = word self.display_word = len(word)*'_' # CORRECT! Instance variables: # WHAT BETTER BE TRUE WHEN WE RE DONE secret_word: [str of lower case letters] display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length 19

Implementing guess() secret_word: [str of lower case letters] # WHAT YOU CAN COUNT ON display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length def apply_ guess(self, letter): """Updates the display_word to reveal all instances of letter as they appear in the secret_word. ( _ is replaced with letter) # JOB OF METHOD letter: the user's guess [1 character string A..Z]""" # ASSUME TRUE secret_word: [str of lower case letters] # WHAT STILL BETTER BE TRUE display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length 20

Implementing guess(), v1 secret_word: [str of lower case letters] # WHAT YOU CAN COUNT ON display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length def apply_ guess(self, letter): """Updates the display_word to reveal all instances of letter as they appear in the secret_word. ( _ is replaced with letter) # JOB OF METHOD letter: the user's guess [1 character string A..Z]""" # ASSUME TRUE for i in list(range(len(self.secret_word))): if self.secret_word[i] == letter: self.display_word = self.display_word[:i] + letter + self.display_word[i:] secret_word: [str of lower case letters] # WHAT STILL BETTER BE TRUE display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length 21

Implementing guess(), v2 secret_word: [str of lower case letters] # WHAT YOU CAN COUNT ON display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length def apply_guess(self, letter): """Updates the display_word to reveal all instances of letter as they appear in the secret_word. ( _ is replaced with letter) # JOB OF METHOD letter: the user's guess [1 character string A..Z]""" # ASSUME TRUE lower_letter = letter.lower() for i in list(range(len(self.secret_word))): if self.secret_word[i] == lower_letter: self.display_word = self.display_word[:i] + lower_letter + self.display_word[i+1:] secret_word: [str of lower case letters] # WHAT STILL BETTER BE TRUE display_word: the letters of secret_word show correctly guessed letters [str of lower case letters and '_'] secret_word and display_word agree on all letters and have same length 22

Planning out a Class: Fraction What attributes? What invariants? What methods? What constructor? class Fraction(object): """Instance is a fraction n/d Attributes: numerator: top [int] denominator: bottom [int > 0] """ def init (self,n=0,d=1): """Init: makes a Fraction""" self.numerator = n self.denominator = d 23

Problem: Doing Math is Unwieldy What We Want 1 2 + 1 3 + 1 4 5 4 Why not use the standard Python math operations? What We Get >>> p = Fraction(1,2) >>> q = Fraction(1,3) >>> r = Fraction(1,4) >>> s = Fraction(5,4) >>> (p.add(q.add(r))).mult(s) Pain! 24

Operator Overloading: Addition class Fraction(object): """Instance attributes: numerator: top [int] denominator: bottom [int > 0]"" def add (self,q): """Returns: Sum of self, q Makes a new Fraction Precondition: q a Fraction""" assert type(q) == Fraction bot = self.denominator*q.denominator top = (self.numerator*q.denominator+ self.denominator*q.numerator) return Fraction(top,bot) >>> p = Fraction(1,2) >>> q = Fraction(3,4) >>> r = p+q >>> r = p. add (q) Python converts to Operator overloading uses method in object on left. 25

Operator Overloading: Multiplication class Fraction(object): """Instance attributes: numerator: top [int] denominator: bottom [int > 0]"" def mul (self,q): """Returns: Product of self, q Makes a new Fraction; does not modify contents of self or q Precondition: q a Fraction""" assert type(q) == Fraction top = self.numerator*q.numerator bot = self.denominator*q.denominator return Fraction(top,bot) >>> p = Fraction(1,2) >>> q = Fraction(3,4) >>> r = p*q >>> r = p. mul (q) Python converts to Operator overloading uses method in object on left. 26

Operator Overloading: Equality By default, == compares folder IDs Can implement eq class Fraction(object): """Instance attributes: numerator: top [int] denominator: bottom [int > 0]""" def eq (self,q): """Returns: True if self, q equal, False if not, or q not a Fraction""" if type(q)!= Fraction: return False left = self.numerator*q.denominator right = self.denominator*q.numerator return left == rght 27