Lecture 19. Using Classes Effectively

Similar documents
Lecture 18. Using Classes Effectively

Lecture 19. Operators and Abstraction

Lecture 18. Methods and Operations

Lecture 21. Programming with Subclasses

Lecture 18: Using Classes Effectively (Chapter 16)

Lecture 21. Programming with Subclasses

Lecture 18. Classes and Types

PREPARING FOR PRELIM 2

PREPARING FOR THE FINAL EXAM

CS Prelim 2 Review Fall 2018

Lecture 20. Subclasses & Inheritance

CS 1110 Prelim 2 November 13th, 2014

Lecture 21. Programming with Subclasses

CS 1110: Introduction to Computing Using Python Loop Invariants

PREPARING FOR PRELIM 1

PREPARING FOR THE FINAL EXAM

Lecture 12. Lists (& Sequences)

CS 1110 Prelim 2 November 6th, 2012

CS Prelim 2 Review Fall 2015

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

CS Prelim 2 Review Fall 2014

CS Prelim 2 Review Fall 2012

CS Prelim 2 Review Fall 2017

CS Lecture 19: Loop invariants

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

Lecture 7. Memory in Python

CSC148 Recipe for Designing Classes

Lecture 11. Asserts and Error Handling

Lecture 9. Memory and Call Stacks

CS 1110 Prelim 2 November 14th, 2013

CSC148 Intro. to Computer Science

Welcome to CSC148! Introduction to Computer Science

CS 1110 Prelim 1 October 4th, 2012

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

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 2 November 10th, 2016

Announcements for This Lecture

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

Data Structures (list, dictionary, tuples, sets, strings)

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

CS Prelim 1 Review Fall 2013

CS 1110 Prelim 1 October 17th, 2013

CSI33 Data Structures

Creating a new data type

Announcements for this Lecture

CS Prelim 1 Review Fall 2018

Lecture 4. Defining Functions

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

CS1 Lecture 12 Feb. 11, 2019

Review 1. Call Frames; Diagramming Objects

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Lecture 11. Asserts and Error Handling

Lecture 5. Defining Functions

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

Announcements for This Lecture

CS 1110 Prelim 1 March 10, 2015

Iteration and For Loops

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

Lecture 10. Memory in Python

CS1210 Lecture 28 Mar. 27, 2019

CS 1110 Prelim 2 November 12th, 2015

Lecture 4. Defining Functions

CS Prelim 1 Review Fall 2017

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

61A LECTURE 14 MULTIPLE REPRESENTATIONS

61A LECTURE 14 MULTIPLE REPRESENTATIONS. Steven Tang and Eric Tzeng July 17, 2013

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Al al-bayt University Prince Hussein Bin Abdullah College for Information Technology Computer Science Department

Lecture 22. While Loops

Lecture 10. Overriding & Casting About

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

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

Java Object Oriented Design. CSC207 Fall 2014

CSE 115 / 503 INTRODUCTION TO COMPUTER SCIENCE I. Dr. Carl Alphonce Dr. Jesse Hartloff

CS 1110 Prelim 1 October 15th, 2015

Object Oriented Programming

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

Lecture 24. GUI Applications

Lecture 13. Call Stacks & Debugging

ASYMPTOTIC COMPLEXITY

OBJECT ORIENTED PROGRAMMING

CS Prelim 1 Review Fall 2013

Lecture 5 Abstract Data Types

CS150 Sample Final. Name: Section: A / B

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 2: Variables & Assignments

CS 1110 Final, December 16th, 2013

Fundamentals of Programming. Week 5- Lecture 1: Intro to Object Oriented Programming (OOP)

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

Lecture 2. Variables & Assignment

Lecture 1. Course Overview, Python Basics

Programming I. Course 9 Introduction to programming

Introduction to Python

CIS192 Python Programming. Robert Rand. August 27, 2015

Programming Languages and Techniques (CIS120)

Lecture 10: Lists and Sequences

Lecture 1. Course Overview, Python Basics

Transcription:

Lecture 19 Using Classes Effectively

Announcements Reading Tuesday: Chapter 18 Thursday reading online Assignments A4 due tonight at Midnight 10 pts per day late Consultants available tonight A5 & A6 posted tomorrow See included micro-deadlines Regrades Today is last day to request Show it to me after class I will verify if it is valid Then request regrade in CMS Prelim, Nov 12 th 7:30-9:00 Material up to November 5 Recursion + Loops + Classes S/U Students are exempt Conflict with Prelim time? Prelim 2 Conflict on CMS 10/29/15 Using Classes Effectively 2

Designing Types From first day of class! Type: set of values and the operations on them int: (set: integers; ops: +,, *, /, ) Time (set: times of day; ops: time span, before/after, ) Worker (set: all possible workers; ops: hire,pay,promote, ) Rectangle (set: all axis-aligned rectangles in 2D; ops: contains, intersect, ) To define a class, think of a real type you want to make Python gives you the tools, but does not do it for you Physically, any object can take on any value Discipline is required to get what you want 10/29/15 Using Classes Effectively 3

Making a Class into a Type 1. Think about what values you want in the set What are the attributes? What values can they have? 2. Think about what operations you want This often influences the previous question To make (1) precise: write a class invariant Statement we promise to keep true after every method call To make (2) precise: write method specifications Statement of what method does/what it expects (preconditions) Write your code to make these statements true! 10/29/15 Using Classes Effectively 4

Planning out a Class class Time(object): """Instances represent times of day. Instance Attributes: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59]""" def init (self, hour, min): """The time hour:min. Pre: hour in 0..23; min in 0..59""" def increment(self, hours, mins): """Move this time <hours> hours and <mins> minutes into the future. Pre: hours is int >= 0; mins in 0..59""" def ispm(self): """Returns: this time is noon or later.""" Class Invariant States what attributes are present and what values they can have. A statement that will always be true of any Time instance. Method Specification States what the method does. Gives preconditions stating what is assumed true of the arguments. 5

Planning out a Class class Rectangle(object): """Instances represent rectangular regions of the plane. Instance Attributes: t: y coordinate of top edge [float] l: x coordinate of left edge [float] b: y coordinate of bottom edge [float] r: x coordinate of right edge [float] For all Rectangles, l <= r and b <= t.""" def init (self, t, l, b, r): """The rectangle [l, r] x [t, b] Pre: args are floats; l <= r; b <= t""" def area(self): """Return: area of the rectangle.""" def intersection(self, other): """Return: new Rectangle describing intersection of self with other.""" Class Invariant States what attributes are present and what values they can have. A statement that will always be true of any Rectangle instance. Method Specification States what the method does. Gives preconditions stating what is assumed true of the arguments. 6

Planning out a Class class Hand(object): """Instances represent a hand in cards. Instance Attributes: cards: cards in the hand [list of card] This list is sorted according to the ordering defined by the Card class.""" def init (self, deck, n): """Draw a hand of n cards. Pre: deck is a list of >= n cards""" def isfullhouse(self): """Return: True if this hand is a full house; False otherwise""" def discard(self, k): """Discard the k-th card.""" Class Invariant States what attributes are present and what values they can have. A statement that will always be true of any Rectangle instance. Method Specification States what the method does. Gives preconditions stating what is assumed true of the arguments. 10/29/15 Using Classes Effectively 7

Implementing a Class All that remains is to fill in the methods. (All?!) When implementing methods: 1. Assume preconditions are true 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 10/29/15 Using Classes Effectively 8

Implementing an Initializer def init (self, hour, min): """The time hour:min. Pre: hour in 0..23; min in 0..59""" This is true to start self.hour = hour self.min = min You put code here Instance variables: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59] This should be true at the end 10/29/15 Using Classes Effectively 9

Implementing a Method Instance variables: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59] def increment(self, hours, mins): """Move this time <hours> hours and <mins> minutes into the future. Pre: hours [int] >= 0; mins in 0..59""" This is true to start What we are supposed to accomplish This is also true to start self.min = self.min + mins self.hour = self.hour + hours? You put code here Instance variables: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59] This should be true at the end 10

Implementing a Method Instance variables: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59] def increment(self, hours, mins): """Move this time <hours> hours and <mins> minutes into the future. Pre: hours [int] >= 0; mins in 0..59""" self.min = self.min + mins self.hour = (self.hour + hours + self.min / 60) self.min = self.min % 60 self.hour = self.hour % 24 This is true to start What we are supposed to accomplish This is also true to start You put code here Instance variables: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59] This should be true at the end 11

Role of Invariants and Preconditions They both serve two purposes Help you think through your plans in a disciplined way Communicate to the user* how they are allowed to use the class Provide the interface of the class interface btw two programmers interface btw parts of an app Important concept for making large software systems Will return to this idea later * who might well be you! in ter face ˈintərˌfās noun 1. a point where two systems, subjects, organizations, etc., meet and interact : the interface between accountancy and the law. chiefly Physics a surface forming a common boundary between two portions of matter or space, e.g., between two immiscible liquids : the surface tension of a liquid at its air/liquid interface. 2. Computing a device or program enabling a user to communicate with a computer. a device or program for connecting two items of hardware or software so that they can be operated jointly or communicate with each other. The Oxford American Dictionary

Implementing a Class All that remains is to fill in the methods. (All?!) When implementing methods: 1. Assume preconditions are true 2. Assume class invariant Easy(ish) true if to we start are the user. 3. Ensure method specification But what is if fulfilled we aren t? 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 10/29/15 Using Classes Effectively 13

Recall: Enforce Preconditions with assert def anglicize(n): """Returns: the anglicization of int n. Precondition: n an int, 0 < n < 1,000,000""" assert type(n) == int, str(n)+' is not an int' assert 0 < n and n < 1000000, str(n)+' is out of range' # Implement method here Check (part of) the precondition (Optional) Error message when precondition violated 10/29/15 Using Classes Effectively 14

Enforce Method Preconditions with assert class Time(object): """Instances represent times of day.""" def init (self, hour, min): """The time hour:min. Pre: hour in 0..23; min in 0..59""" assert type(hour) == int assert 0 <= hour and hour < 24 assert type(min) == int assert 0 <= min and min < 60 def increment(self, hours, mins): """Move this time <hours> hours and <mins> minutes into the future. Pre: hours is int >= 0; mins in 0..59"" assert type(hour) == int assert type (min) == int assert hour >= 0 and assert 0 <= min and min < 60 Instance Attributes: hour: hour of day [int in 0..23] min: minute of hour [int in 0..59] Initializer creates/initializes all of the instance attributes. Asserts in initializer guarantee the initial values satisfy the invariant. Asserts in other methods enforce the method preconditions. 15

Hiding Methods From Access Put underscore in front of a method will make it hidden Will not show up in help() But it is still there Hidden methods Can be used as helpers inside of the same class But it is bad style to use them outside of this class Can do same for attributes Underscore makes it hidden Do not use outside of class HIDDEN class Fraction(object): """Instance attributes: numerator: top [int] denominator: bottom [int > 0]""" def _is_denominator(self,d): """Return: True if d valid denom""" return type(d) == int and d > 0 Helper method def init (self,n=0,d=1): assert self._is_denominator(d) self.numerator = n self.denominator = d 10/29/15 Using Classes Effectively 16

Enforcing Invariants class Fraction(object): """Instance attributes: """ numerator: top [int] denominator: bottom [int > 0] These are just comments! >>> p = Fraction(1,2) Invariants: Properties that are always true. >>> p.numerator = 'Hello' How do we prevent this? Idea: Restrict direct access Only access via methods Use asserts to enforce them Examples: def getnumerator(self): """Returns: numerator""" return self.numerator def setnumerator(self,value): """Sets numerator to value""" assert type(value) == int self.numerator = value 10/29/15 Using Classes Effectively 17

Data Encapsulation Idea: Force the user to only use methods Do not allow direct access of attributes Setter Method Used to change an attribute Replaces all assignment statements to the attribute Bad: >>> f.numerator = 5 Good: >>> f.setnumerator(5) Getter Method Used to access an attribute Replaces all usage of attribute in an expression Bad: >>> x = 3*f.numerator Good: >>> x = 3*f.getNumerator() 10/29/15 Using Classes Effectively 18

Data Encapsulation Getter Setter class Fraction(object): """Instance attributes: _numerator: top [int] _denominator: bottom [int > 0]""" def getdenomenator(self): """Returns: numerator attribute""" return self._denomenator def setdenomenator(self, d): """Alters denomenator to be d Pre: d is an int > 0""" assert type(d) == int assert 0 < d self._denominator = d Do this for all of your attributes Naming Convention The underscore means should not access the attribute directly. Precondition is same as attribute invariant. 10/29/15 Using Classes Effectively 19

Mutable vs. Immutable Attributes Mutable Can change value directly If class invariant met Example: t.color Has both getters and setters Setters allow you to change Enforce invariants w/ asserts Immutable Can t change value directly May change behind scenes Example: t.x Has only a getter No setter means no change Getter allows limited access May ask you to differetiate on the exam 10/29/15 Using Classes Effectively 20

Structure of a Proper Python Class class Fraction(object): """Instances represent a Fraction Attributes: _numerator: [int] _denominator: [int > 0]""" def getnumerator(self): """Returns: Numerator of Fraction""" def init (self,n=0,d=1): """Initializer: makes a Fraction""" def add (self,q): """Returns: Sum of self, q""" def normalize(self): """Puts Fraction in reduced form""" Docstring describing class Attributes are all hidden Getters and Setters. Initializer for the class. Defaults for parameters. Python operator overloading Normal method definitions 10/29/15 Using Classes Effectively 21

Exercise: Design a (2D) Circle What are the attributes? What is the bare minimum we need? What are some extras we might want? What are the invariants? What are the methods? With just the one circle? With more than one circle? 10/29/15 Using Classes Effectively 22

Advanced Topic Warning! The following will not be on the exam If you ask Will this be on the Exam we will be. 10/29/15 Using Classes Effectively 23

Properties: Invisible Setters and Getters class Fraction(object): """Instance attributes: _numerator: [int] _denominator: [int > 0]""" @property def numerator(self): """Numerator value of Fraction Invariant: must be an int""" return self._numerator @numerator.setter def numerator(self,value): assert type(value) == int self._numerator = value >>> p = Fraction(1,2) >>> x = p.numerator >>> x = p.numerator() >>> p.numerator = 2 >>> p.numerator(2) Python converts to Python converts to 10/29/15 Using Classes Effectively 24

Properties: Invisible Setters and Getters class Fraction(object): """Instance attributes: _numerator: [int] _denominator: [int > 0]""" @property def numerator(self): """Numerator value of Fraction Invariant: must be an int""" return self._numerator @numerator.setter def numerator(self,value): assert type(value) == int self._numerator = value Specifies that next method is the getter for property of the same name as the method Docstring describing property Property uses hidden attribute. Specifies that next method is the setter for property whose name is numerator. 10/29/15 Using Classes Effectively 25

Properties: Invisible Setters and Getters class Fraction(object): """Instance attributes: _numerator: [int] _denominator: [int > 0]""" @property def numerator(self): """Numerator value of Fraction Invariant: must be an int""" return self._numerator @numerator.setter def numerator(self,value): assert type(value) == int self._numerator = value Goal: Data Encapsulation Protecting your data from other, clumsy users. Only the getter is required! If no setter, then the attribute is immutable. Replace Attributes w/ Properties (Users cannot tell difference) 10/29/15 Using Classes Effectively 26