Lecture 18. Methods and Operations

Similar documents
Lecture 18. Using Classes Effectively

Lecture 19. Operators and Abstraction

Announcements for This Lecture

Lecture 18: Using Classes Effectively (Chapter 16)

Announcements for This Lecture

Lecture 21. Programming with Subclasses

Lecture 19. Using Classes Effectively

Lecture 18. Classes and Types

Lecture 21. Programming with Subclasses

Review 2. Classes and Subclasses

Lecture 17: Classes (Chapter 15)

Lecture 21. Programming with Subclasses

Class Design (Section 9 of 17)

CS 1110 Prelim 2 November 10th, 2016

COMPSCI 105 S Principles of Computer Science. Classes 3

CS 1110: Introduction to Computing Using Python Loop Invariants

PREPARING FOR PRELIM 2

Creating a new data type

Lecture 20. Subclasses & Inheritance

PREPARING FOR THE FINAL EXAM

CS 1110 Prelim 2 November 12th, 2015

PREPARING FOR PRELIM 1

CSC148: Week 1

CSC148-Section:L0301

CS 1110 Prelim 2 November 13th, 2014

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

CS Prelim 2 Review Fall 2018

CS 1110 Prelim 2 November 12th, 2015

Lecture 7. Memory in Python

CS 1110 Prelim 2 November 14th, 2013

Readings for This Lecture

Week 2. Classes and Objects

Announcements for the Class

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

Lecture 19: Subclasses & Inheritance (Chapter 18)

Welcome to CSC148! Introduction to Computer Science

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

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

CSI33 Data Structures

CS 1110 Prelim 2 November 6th, 2012

Lecture 9. Memory and Call Stacks

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

CSC148 Recipe for Designing Classes

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 22 CLASS MICHAEL GROSSBERG

CS 1110 Prelim 1 October 4th, 2012

Announcements for this Lecture

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

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

CS Lecture 19: Loop invariants

Object Oriented Programming in Python 3

Iteration and For Loops

CS Prelim 1 Review Fall 2018

Iterators & Generators

String Representations

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

CS1110 Lab 6 (Mar 17-18, 2015)

PREPARING FOR THE FINAL EXAM


Lecture 24: Loop Invariants

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

CSC148 Intro. to Computer Science

Lecture 17: Classes (Chapter 15)

CS61A Lecture 15 Object Oriented Programming, Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 12, 2012

OBJECT ORIENTED PROGRAMMING

Writing a Fraction Class

CS Prelim 2 Review Fall 2017

5.6 Rational Equations

CS Prelim 2 Review Fall 2012

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Review 3. Exceptions and Try-Except Blocks

CS Lecture 25: Models, Views, Controllers, and Games. Announcements

Lecture 2: Variables & Assignments

CS Prelim 2 Review Fall 2015

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

Variable and Data Type I

CSC148H Week 1 >> COURSE INFORMATION, RECAP, OOP. Sadia Rain Sharmin Week of May 7, 2018

CS Prelim 2 Review Fall 2014

CS 1110 Prelim 2 April 26th, 2016

CSE 115. Introduction to Computer Science I

CS Prelim 1 Review Fall 2017

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

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

CSC148: Week 2

Lecture 3. Input, Output and Data Types

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

CSc 110, Autumn Lecture 30: Methods. Adapted from slides by Marty Stepp and Stuart Reges

CS Prelim 1 Review Fall 2013

Lecture 11: Iteration and For-Loops

ENGR 102 Engineering Lab I - Computation

CompSci 101 Introduction to Computer Science

Lecture 1. Types, Expressions, & Variables

CS 1110 Prelim 1 October 15th, 2015

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

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

Lecture 8. Reviewing Methods; Wrapper Classes

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

Lecture 4. Defining Functions

Abstract Data Types Chapter 1

Programming with Python

Transcription:

Lecture 18 Methods and Operations

Announcements for This Lecture Assignments A4 Due Thursday at midnight Hopefully you are on Task 4 Extra consultants available Will post A5 on Thursday Written assignment like A2 Needs material from next Tues Will also post A6 as well Not due until November 19 Want to avoid exam crunch Lab this Week Simple class exercise Fill in predefined methods Setting you up for A6 Exams Moved to handback room Located in Gates 216 Open 12-4:30 daily Regrades still open this week 10/27/15 Methods and Operations 2

Important! YES class Point3(object): """Instances are 3D points Attributes: x: x-coord [float] y: y-coord [float] z: z-coord [float]""" 3.0-Style Classes Well-Designed class Point3: NO """Instances are 3D points Attributes: x: x-coord [float] y: y-coord [float] z: z-coord [float]""" Old-Style Classes Very, Very Bad 10/27/15 Methods and Operations 3

Case Study: Fractions Want to add a new type Values are fractions: ½, ¾ Operations are standard multiply, divide, etc. Example: ½*¾ = ⅜ Can do this with a class Values are fraction objects Operations are methods Example: simplefrac.py 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 10/27/15 Methods and Operations 4

Problem: Doing Math is Unwieldy What We Want 1 2 + 1 3 + 1 4 5 4 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) This is confusing! 10/27/15 Methods and Operations 5

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) This is confusing! 10/27/15 Methods and Operations 6

Recall: The init Method two underscores w = Worker('Obama', 1234, None) def init (self, n, s, b): "" Initializer: creates a Worker Has last name n, SSN s, and boss b Precondition: n a string, s an int in range 0..999999999, and b either a Worker or None. self.lname = n self.ssn = s self.boss = b Called by the constructor id8 Worker lname 'Obama' ssn 1234 boss None 10/27/15 Methods and Operations 7

Recall: The init Method two underscores w = Worker('Obama', 1234, None) def init (self, n, s, b): "" Initializer: creates a Worker Has last name n, SSN s, and boss b Precondition: n a string, s an int in range 0..999999999, and b either a Worker or None. self.lname = n self.ssn = s self.boss = b Are there other special methods that we can use? 10/27/15 Methods and Operations 8

Example: Converting Values to Strings str() Function Usage: str(<expression>) Evaluates the expression Converts it into a string How does it convert? str(2) '2' str(true) 'True' str('true') 'True' str(point3()) '(0.0,0.0,0.0)' Backquotes Usage: `<expression>` Evaluates the expression Converts it into a string How does it convert? `2` '2' `True` 'True' `'True'` "'True'" `Point3()` "<class 'Point3'> (0.0,0.0,0.0)" 10/27/15 Methods and Operations 9

Example: Converting Values to Strings str() Function Usage: str(<expression>) Evaluates the expression Converts it into a string How does it convert? What type is str(2) '2' this value? str(true) 'True' str('true') 'True' str(point3()) '(0.0,0.0,0.0)' Backquotes Backquotes are for unambigious representation Usage: `<expression>` Evaluates the expression Converts it into a string How does it convert? The value s `2` '2' type is clear `True` 'True' `'True'` "'True'" `Point3()` "<class 'Point3'> (0.0,0.0,0.0)" 10/27/15 Methods and Operations 10

What Does str() Do On Objects? Does NOT display contents >>> p = Point3(1,2,3) >>> str(p) '<Point3 object at 0x1007a90>' Must add a special method str for str() repr for backquotes Could get away with just one Backquotes require repr str() can use repr (if str is not there) class Point3(object): """Instances are points in 3d space""" def str (self): """Returns: string with contents""" return '('+self.x + ',' + self.y + ',' + self.z + ')' def repr (self): """Returns: unambiguous string""" return str(self. class )+ str(self) 10/27/15 Methods and Operations 11

What Does str() Do On Objects? Does NOT display contents >>> p = Point3(1,2,3) >>> str(p) '<Point3 object at 0x1007a90>' Must add a special method str for str() repr for backquotes Could get away with just one Backquotes require repr str() can use repr (if str is not there) class Point3(object): """Instances are points in 3d space""" def str (self): """Returns: string with contents""" return '('+self.x + ',' + self.y + ',' + self.z + ')' Gives the def repr (self): class name """Returns: unambiguous string""" return str(self. class )+ str(self) repr using str as helper 10/27/15 Methods and Operations 12

Special Methods in Python Have seen three so far init for initializer str for str() repr for backquotes Start/end with 2 underscores This is standard in Python Used in all special methods Also for special attributes For a complete list, see http://docs.python.org/reference /datamodel.html class Point3(object): """Instances are points in 3D space""" def init (self,x=0,y=0,z=0): """Initializer: makes new Point3""" def str (self,q): """Returns: string with contents"" def repr (self,q): """Returns: unambiguous string"" 10/27/15 Methods and Operations 13

Returning to Fractions What We Want 1 2 + 1 3 + 1 4 5 4 Why not use the standard Python math operations? Operator Overloading Python has methods that correspond to built-in ops add corresponds to + mul corresponds to * Not implemented by default Implementing one allows you to use that op on your objects Called operator overloading Changes operator meaning 10/27/15 Methods and Operations 14

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. 10/27/15 Methods and Operations 15

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. 10/27/15 Methods and Operations 16

Comparing Objects for Equality Earlier in course, we saw == compare object contents This is not the default Default: folder names Must implement eq Operator overloading! Not limited to simple attribute comparison Ex: cross multiplying 4 1 2 4 2 4 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 rght = self.denominator*q.numerator return left == rght 10/27/15 Methods and Operations 17

Issues With Overloading == Overloading == does not also overload comparison!= Must implement ne Why? Will see later But (not x == y) is okay! What if you still want to compare Folder names? Use is operator on variables (x is y) True if x, y contain the same folder name Check if variable is empty: x is None (x == None is bad) class Fraction(object): 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 rght = self.denominator*q.numerator return left == rght def ne (self,q): """Returns: False if self, q equal, True if not, or q not a Fraction" return not self == q 10/27/15 Methods and Operations 18

is Versus == p is q evaluates to False Compares folder names Cannot change this p == q evaluates to True But only because method eq compares contents p id2 id2 Point q id3 id3 Point x 2.2 x 2.2 y 5.4 y 5.4 z 6.7 z 6.7 Always use (x is None) not (x == None) 10/27/15 Methods and Operations 19