Lecture 21. Programming with Subclasses

Similar documents
Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses

Lecture 18. Classes and Types

Review 3. Exceptions and Try-Except Blocks

CS Lecture 26: Grab Bag. Announcements

Lecture 18. Methods and Operations

Lecture 20. Subclasses & Inheritance

Lecture 19. Operators and Abstraction

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Lecture 18: Using Classes Effectively (Chapter 16)

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Exceptions CS GMU

PREPARING FOR PRELIM 2

isinstance and While Loops

Lecture 11. Asserts and Error Handling

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

Lecture #12: Quick: Exceptions and SQL

Exceptions & a Taste of Declarative Programming in SQL

CS61A Lecture 32. Amir Kamil UC Berkeley April 5, 2013

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

Lecture 19. Using Classes Effectively

PREPARING FOR THE FINAL EXAM

CS 11 python track: lecture 2

Pairs and Lists. (cons 1 2) 1 2. (cons 2 nil) 2 nil. Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) ( ) (Demo)

CS 1110 Prelim 2 November 14th, 2013

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

Lecture 19: Subclasses & Inheritance (Chapter 18)

Review 2. Classes and Subclasses

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

CSE : Python Programming

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

Announcements for This Lecture

Announcements for This Lecture

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

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

PREPARING FOR THE FINAL EXAM

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

Lecture 11. Asserts and Error Handling

61A Lecture 25. Friday, October 28

Week 2. Classes and Objects

CS 1110: Introduction to Computing Using Python Loop Invariants

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

CSC148 Recipe for Designing Classes

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

CS Prelim 2 Review Fall 2017

Class extension and. Exception handling. Genome 559

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

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

PREPARING FOR PRELIM 1

CS Prelim 2 Review Fall 2018

Class extension and. Exception handling. Genome 559

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Exceptions & error handling in Python 2 and Python 3

Exception Handling and Debugging


Object-Oriented Python

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

Final Exam Version A

CS 11 python track: lecture 3. n Today: Useful coding idioms

LECTURE 4 Python Basics Part 3

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

CS 11 python track: lecture 4

Lecture 17: Classes (Chapter 15)

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Lecture 17: Classes (Chapter 15)

CS Prelim 2 Review Fall 2012

New York University School of Continuing and Professional Studies Management and Information Technology. Advanced Python. Homework, Session 5


CS 1110 Prelim 1 October 4th, 2012

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

CS Prelim 2 Review Fall 2015

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

Exam #3, Form 3 A CSE 231 Fall 2015 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

CS Prelim 2 Review Fall 2014

CS1110 Lab 6 (Mar 17-18, 2015)

Chapter 9: Dealing with Errors

CS 1110 Prelim 2 November 12th, 2015

SCHEME INTERPRETER GUIDE 4

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

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

CS 1110 Prelim 2 November 12th, 2015

Python for Astronomers. Errors and Exceptions

Sorting and Searching

Beyond Blocks: Python Session #1

COMP 204: Sets, Commenting & Exceptions

Lecture 7. Memory in Python

What we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

Fundamentals of Programming. Week 11 - Lecture 1: OOP Part 2.

Lecture 4. Defining Functions

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

Lecture 22. While Loops

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

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.

Structure and Flow. CS 3270 Chapter 5

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

CSC148: Week 1

CS1210 Lecture 28 Mar. 27, 2019

Introduction to: Computers & Programming: Exception Handling

Transcription:

Lecture 21 Programming with Subclasses

Announcements for This Lecture Assignments Prelim 2 A4 is now graded Mean: 90.4 Median: 93 Std Dev: 10.6 Mean: 9 hrs Median: 8 hrs Std Dev: 4.1 hrs A5 is also graded Mean: 46.4 Median: 49 A: 47 (74%), B: 40 (19%) Thursday, 5:15 or 7:30 K Z at 5:15pm A J at 7:30 pm See website for room Conflicts received e-mail ANOTHER review Wed. Run by the URMC Open up to everyone Solutions posted in CMS 11/6/18 Programming with Subclasses 2

A Problem with Subclasses class Fraction(object): """Instances are normal fractions n/d Instance attributes: numerator: top [int] denominator: bottom [int > 0] """ class BinaryFraction(Fraction): """Instances are fractions k/2 n Instance attributes are same, BUT: numerator: top [int] denominator: bottom [= 2 n, n 0] """ def init (self,k,n): """Make fraction k/2 n """ assert type(n) == int and n >= 0 super(). init (k,2 ** n) >>> p = Fraction(1,2) >>> q = BinaryFraction(1,2) # 1/4 >>> r = p*q Python converts to >>> r = p. mul (q) # ERROR mul has precondition type(q) == Fraction 11/6/18 Programming with Subclasses 3

The isinstance Function isinstance(<obj>,<class>) True if <obj> s class is same as or a subclass of <class> False otherwise Example: isinstance(e,executive) is True isinstance(e,employee) is True isinstance(e,object) is True isinstance(e,str) is False Generally preferable to type Works with base types too! e id4 id4 Executive _name 'Fred' _start 2012 _salary 0.0 _bonus 0.0 object Employee Executive 11/6/18 Programming with Subclasses 4

isinstance and Subclasses >>> e = Employee('Bob',2011) >>> isinstance(e,executive)??? A: True B: False C: Error D: I don t know e id5 id5 Employee _name 'Bob' _start 2012 _salary 50k object Employee Executive 11/6/18 Programming with Subclasses 5

isinstance and Subclasses >>> e = Employee('Bob',2011) >>> isinstance(e,executive)??? A: True B: False Correct C: Error D: I don t know object Employee Executive means extends or is an instance of 11/6/18 Programming with Subclasses 6

Fixing Multiplication class Fraction(object): """Instance attributes: numerator [int]: top denominator [int > 0]: bottom""" 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 isinstance(q, Fraction) top = self.numerator*q.numerator bot = self.denominator*q.denominator return Fraction(top,bot) >>> p = Fraction(1,2) >>> q = BinaryFraction(1,2) # 1/4 >>> r = p*q Python converts to >>> r = p. mul (q) # OKAY Can multiply so long as it has numerator, denominator 11/6/18 Programming with Subclasses 7

Error Types in Python def foo(): assert 1 == 2, 'My error' def foo(): x = 5 / 0 >>> foo() AssertionError: My error Class Names >>> foo() ZeroDivisionError: integer division or modulo by zero 11/6/18 Programming with Subclasses 8

Error Types in Python def foo(): assert 1 == 2, 'My error' >>> foo() AssertionError: My error Class Names def foo(): Information about an error is x stored = 5 / inside 0 an object. The error type is the class of the error object. >>> foo() ZeroDivisionError: integer division or modulo by zero 11/6/18 Programming with Subclasses 9

Error Types in Python All errors are instances of class BaseException This allows us to organize them in a hierarchy BaseException init (msg) str () Exception(BE) BaseException Exception id4 AssertionError 'My error' AssError(SE) AssertionError means extends or is an instance of 11/6/18 Programming with Subclasses 10

Error Types in Python All errors are instances of class BaseException This allows us to organize them in a hierarchy BaseException init (msg) str () Exception(BE) BaseException All of these are actually Exception empty! Why? id4 AssertionError 'My error' AssError(SE) AssertionError means extends or is an instance of 11/6/18 Programming with Subclasses 11

Python Error Type Hierarchy SystemExit BaseException Exception Argument has wrong type (e.g. float([1])) Argument has wrong value (e.g. float('a')) AssertionError AttributeError ArithmeticError IOError TypeError ValueError ZeroDivisionError OverflowError http://docs.python.org/ library/exceptions.html Why so many error types? 11/6/18 Programming with Subclasses 12

Recall: Recovering from Errors try-except blocks allow us to recover from errors Do the code that is in the try-block Once an error occurs, jump to the catch Example: try: val = input() x = float(val) # get number from user # convert string to float print('the next number is '+str(x+1)) except: print('hey! That is not a number!') might have an error executes if have an error 11/6/18 Programming with Subclasses 13

Handling Errors by Type try-except blocks can be restricted to specific errors Doe except if error is an instance of that type If error not an instance, do not recover Example: try: val = input() x = float(val) # get number from user # convert string to float print('the next number is '+str(x+1)) except ValueError: print('hey! That is not a number!') May have IOError May have ValueError Only recovers ValueError. Other errors ignored. 11/6/18 Programming with Subclasses 14

Handling Errors by Type try-except blocks can be restricted to specific errors Doe except if error is an instance of that type If error not an instance, do not recover Example: try: val = input() x = float(val) # get number from user # convert string to float print('the next number is '+str(x+1)) except IOError: print('check your keyboard!') May have IOError May have ValueError Only recovers IOError. Other errors ignored. 11/6/18 Programming with Subclasses 15

Creating Errors in Python Create errors with raise Usage: raise <exp> exp evaluates to an object An instance of Exception Tailor your error types ValueError: Bad value TypeError: Bad type Still prefer asserts for preconditions, however Compact and easy to read def foo(x): assert x < 2, 'My error' def foo(x): if x >= 2: m = 'My error' err = AssertionError(m) raise err Identical 11/6/18 Programming with Subclasses 16

Creating Errors in Python Create errors with raise Usage: raise <exp> exp evaluates to an object An instance of Exception Tailor your error types ValueError: Bad value TypeError: Bad type Still prefer asserts for preconditions, however Compact and easy to read def foo(x): assert x < 2, 'My error' def foo(x): if x >= 2: m = 'My error' err = TypeError(m) raise err Identical 11/6/18 Programming with Subclasses 17

Raising and Try-Except def foo(): x = 0 try: raise Exception() x = 2 except Exception: x = 3 return x The value of foo()? A: 0 B: 2 C: 3 D: No value. It stops! E: I don t know 11/6/18 Programming with Subclasses 18

Raising and Try-Except def foo(): x = 0 try: raise Exception() x = 2 except Exception: x = 3 return x The value of foo()? A: 0 B: 2 C: 3 Correct D: No value. It stops! E: I don t know 11/6/18 Programming with Subclasses 19

Raising and Try-Except def foo(): x = 0 try: raise Exception() x = 2 except BaseException: x = 3 return x The value of foo()? A: 0 B: 2 C: 3 D: No value. It stops! E: I don t know 11/6/18 Programming with Subclasses 20

Raising and Try-Except def foo(): x = 0 try: raise Exception() x = 2 except BaseException: x = 3 return x The value of foo()? A: 0 B: 2 C: 3 Correct D: No value. It stops! E: I don t know 11/6/18 Programming with Subclasses 21

Raising and Try-Except def foo(): x = 0 try: raise Exception() x = 2 except AssertionError: x = 3 return x The value of foo()? A: 0 B: 2 C: 3 D: No value. It stops! E: I don t know 11/6/18 Programming with Subclasses 22

Raising and Try-Except def foo(): x = 0 try: raise Exception() x = 2 except AssertionError: x = 3 return x The value of foo()? A: 0 B: 2 C: 3 D: No value. Correct It stops! E: I don t know Python uses isinstance to match Error types 11/6/18 Programming with Subclasses 23

Creating Your Own Exceptions class CustomError(Exception): """An instance is a custom exception""" pass This is all you need No extra fields No extra methods No constructors Inherit everything Only issues is choice of parent error class. Use Exception if you are unsure what. 11/6/18 Programming with Subclasses 24

Handling Errors by Type try-except can put the error in a variable Example: try: val = input() x = float(val) # get number from user # convert string to float print('the next number is '+str(x+1)) except ValueError as e: print(e.args[0]) print('hey! That is not a number!') Some Error subclasses have more attributes 11/6/18 Programming with Subclasses 25

Accessing Attributes with Strings hasattr(<obj>,<name>) Checks if attribute exists getattr(<obj>,<name>) Reads contents of attribute delattr(<obj>,<name>) Deletes the given attribute setattr(<obj>,<name>,<val>) Sets the attribute value <obj>. dict List all attributes of object 11/6/18 Programming with Subclasses 26 id1 x y z Point3 2.0 3.0 5.0 id2 'x' 'y' 'z' Treat object like dictionary 2.0 3.0 5.0 dict

Typing Philosophy in Python Duck Typing: Type object is determined by its methods and properties Not the same as type() value Preferred by Python experts Implement with hasattr() hasattr(<object>,<string>) Returns true if object has an attribute/method of that name This has many problems The name tells you nothing about its specification class Fraction(object): """Instance attributes: numerator [int]: top denominator [int > 0]: bottom""" 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 11/6/18 Programming with Subclasses 27

Typing Philosophy in Python Duck Typing: Type object is determined by its methods and properties Not the same as type() value Preferred by Python experts Implement with hasattr() hasattr(<object>,<string>) Returns true if object has an attribute/method of that name This has many problems The name tells you nothing about its specification class Fraction(object): """Instance attributes: numerator [int]: top denominator [int > 0]: bottom""" def eq (self,q): """Returns: True if self, q equal, False if not, or q not a Fraction""" if (not (hasattr(q,'numerator') and hasattr(q,'denomenator')): return False left = self.numerator*q.denominator rght = self.denominator*q.numerator return left == rght 11/6/18 Programming with Subclasses 28

Duck Typing: Typing Philosophy in Python Type object is determined by its methods and properties Not the same as type() value Preferred by Python experts Compares anything with Implement numerator with hasattr() & denominator hasattr(<object>,<string>) Returns true if object has an attribute/method of that name This has many problems The name tells you nothing about its specification class Fraction(object): """Instance attributes: numerator [int]: 11/6/18 Programming with Subclasses 29 top denominator [int > 0]: bottom""" def eq (self,q): """Returns: True if self, q equal, False if not, or q not a Fraction""" if (not (hasattr(q,'numerator') and hasattr(q,'denomenator')): return False left = self.numerator*q.denominator rght = self.denominator*q.numerator return left == rght

Duck Typing: Typing Philosophy in Python Type object is determined by its methods and properties How to properly implement/use typing Not the same is a as major type() debate value in language design Preferred by What Pythonwe experts really care def about eq (self,q): is Implement with specifications hasattr() (and invariants) hasattr(<object>,<string>) Types are a shorthand for this Returns true Different object typing has an styles trade ease-of-use attribute/method with overall of that program name robustness/safety return False This has many problems The name tells you nothing about its specification class Fraction(object): """Instance attributes: numerator [int] : 11/6/18 Programming with Subclasses 30 top denominator [int > 0]: bottom""" """Returns: True if self, q equal, False if not, or q not a Fraction""" if (not (hasattr(q,'numerator') and hasattr(q,'denomenator')): left = self.numerator*q.denominator rght = self.denominator*q.numerator return left == rght

Typing Philosophy in Python Duck Typing: Type object is determined by its methods and properties Not the same as type() value Preferred by Python experts Implement with hasattr() hasattr(<object>,<string>) Returns true if object has an attribute/method of that name This has many problems The name tells you nothing about its specification class Employee(object): """An Employee with a salary" " def eq (self,other): if (not (hasattr(other,'name') and hasattr(other,'start') and hasattr(other,'salary')) return False return (self.name == other.name and self.start == other.start and self.salary == other.salary) 11/6/18 Programming with Subclasses 31