61A Lecture 16. Friday, October 11



Similar documents
Lecture 15: Inheritance

MATCHDAY September 2014

CSC 314: Operating Systems Spring 2005

Python for Rookies. Example Examination Paper

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

SUMMER TIMETABLE CAMBRIDGE

(Part 2) Lunch Block 7 1:05 PM 2:27 PM

Your Family Tree. Afamily tree is a wonderful way to introduce. We re 4.1 LESSON. How we re related

Chapter 13 - Inheritance

International University of Monaco 27/04/ :55 - Page 1. Monday 30/04 Tuesday 01/05 Wednesday 02/05 Thursday 03/05 Friday 04/05 Saturday 05/05

International University of Monaco 21/05/ :01 - Page 1. Monday 30/04 Tuesday 01/05 Wednesday 02/05 Thursday 03/05 Friday 04/05 Saturday 05/05

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

Foundations of Programming

General Psychology, PSY 101

Academic Calendar for Faculty

FINAL SCHEDULE YEAR 1 AUGUST WEEK 1

NAB cash back Terms and Conditions. effective

ACTG 051A: Intermediate Accounting 1A Foothill College, Summer 2015

D06 PROGRAMMING with JAVA

TIME MANAGEMENT By Ann Marie Ross Created for CLASS and ECLP Revised Fall 2005 by Peggy Ozaki

CSCI 4250/6250 Fall 2015 Computer and Network Security. Instructor: Prof. Roberto Perdisci

Court Services Online Registration Guide

CS193j, Stanford Handout #10 OOP 3

Academic Calendar - UNDERGRADUATE. All Other Classes Begin; Late Registration Begins. Last Day to Drop a Class without a W on Transcript;

5. Advanced Object-Oriented Programming Language-Oriented Programming

Math 35 Section Spring Class meetings: 6 Saturdays 9:00AM-11:30AM (on the following dates: 2/22, 3/8, 3/29, 5/3, 5/24, 6/7)

How to Create a Fundraising Team

On Line Banking Agreement and Application for Commercial Accounts

CS 1301 Homework 6 Scribbler Line Sensing Due: Friday March 13th, before 11:55pm PM EST. Out of 130 points Files to submit:

International University of Monaco 11/06/ :27 - Page 1. Monday 30/04 Tuesday 01/05 Wednesday 02/05 Thursday 03/05 Friday 04/05 Saturday 05/05

Pinning and Graduation Ceremony at St. Aloysius. Fall Semester and Term B Classes End. St Francis School of Radiography Seniors Spring Semester

Criminology CRJU 2004 B Department of Criminal Justice College of Arts & Letters University of North Georgia

PSYC General Psychology Course Syllabus

ACCESS Nursing Programs Session 1 Center Valley Campus Only 8 Weeks Academic Calendar 8 Weeks

ACCESS Nursing Programs Session 1 Center Valley Campus Only 8 Weeks Academic Calendar 8 Weeks

ACTG 051B: Intermediate Accounting 1B Foothill College, Summer 2015

Intuit QuickBooks Direct Deposit Getting Started Guide

USC VITERBI SCHOOL OF ENGINEERING INFORMATICS PROGRAM

BOLLI Registration Information and FAQ

School of Business ACCT2105/BUSI0027 (Subclasses A, B, C) Introduction to Management Accounting/ Management Accounting I Course Syllabus

ACC 7145: ACCOUNTING SYSTEMS DESIGN AND CONTROL SYLLABUS FOR SUMMER SESSION 2014

CSci 4211: Data Communications and Computer Networks. Time: Tuesday and Thursday 8:15 to 9:30 am Location: Phyics 170 Spring 2015, 3 Credits

Finance 471: DERIVATIVE SECURITIES Fall 2015 Prof. Liang Ma University of South Carolina, Moore School of Business


International University of Monaco 12/04/ :50 - Page 1. Monday 30/01 Tuesday 31/01 Wednesday 01/02 Thursday 02/02 Friday 03/02 Saturday 04/02

INFO 241 INTRODUCTION TO DATABASE MANAGEMENT AND PROGRAMMING

I. PREREQUISITE For information regarding prerequisites for this course, please refer to the Academic Course Catalog.

CRIJ 1301 Introduction to Criminal Justice Course Syllabus: Summer 2015

: Foundations of Game Programming

TheBANK of Edwardsville s Online Banking Agreement

Medical Biochemistry BC 362 Fall 2014

GLOBAL EXPRESS PAYMENT CENTER

How Do You Manage Your Time?

Rutgers University, Department of Psychology Developmental Psychology Winter Office Hours Office Phone

Savings General Savings Account Terms & Conditions Effective 1 st June 2015

MYPY: A PYTHON VARIANT WITH SEAMLESS DYNAMIC AND STATIC TYPING. Jukka Lehtosalo University of Cambridge Computer Laboratory

MTH (seated)/277 V01 (online): Vector Calculus, Fall 2015

Carleton University School of Computer Science COMP Computer graphics Fall 2015

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Course Outline 2015 FINANCE 261: INTRODUCTION TO INVESTMENTS (15 POINTS) Semester 2 (1155)

DSCI 3710 Syllabus: Spring 2015

UNITED STATES PATENT AND TRADEMARK OFFICE An Agency of the Department of Commerce

Trading Calendar - East Capital UCITS Funds

T TH 7:30am to 8:30am. Exam on Thursday. T TH 1:30pm to 2:20pm. MWF 8:30am to 9:30am. T TH 11:30am to 12:30pm

PA 750: Financial Management in Public Service Tuesday, 6:00-8:45 pm DTC Lab 617

Introduction: How does a student get started? How much time does this course require per week?

APPLICATION FORM INTERNATIONAL STUDENT GENERAL ENGLISH / ELICOS COURSES VOCATIONAL COURSES AGENT S STAMP. STUDENT ID (if applicable)

BA 561: Database Design and Applications Acct 565: Advanced Accounting Information Systems Syllabus Spring 2015

APPROVED - CAMBRIAN COLLEGE ACADEMIC SCHEDULE September August 2016

Key features of the HSBC InvestDirect Stocks and Shares ISA (Individual Savings Account)

UNIVERSITY OF BRIDGEPORT School of Engineering SPRING 2016 COURSE OUTLINE. TCMG/MEEG 573 Supply Chain Management

Psychology 150-D01 Winter 2010 Child Development Online. Instructor: Grace Chan Phone: Office: F352

FAST TRACK SUCCESS SYSTEM

Engineering Problem Solving and Programming (CS 1133)


General Psychology Course Syllabus - INMON. PSYC 2301 General Psychology. Psychology. Behavioral Sciences Department. Division of Arts and Sciences

How can I keep track of the money in my checking account?

ICOM 4015: Advanced Programming

Interest Rates. variable rate savings and current accounts. Be more than a number

Oakland Community College MAT A1503 Calculus I Fall Semester, Instructor Jeremy JJ Mertz Office C-245

DimeOnLine BillPay Frequently Asked Questions

ITNW 1337 Introduction to the Internet Course Syllabus: Spring 2015

WELCOME TO STUDENTS OF PHYSICS 111 SPRING 2006 THIS SHEET CONTAINS INFORMATION ABOUT PHYSICS 111.

English 3352, Business Communications Online

How To Pass A Customer Service Course At Tstc

Academic Calendars. Term I (20081) Term II (20082) Term III (20083) Weekend College. International Student Admission Deadlines

Nombre: RED GREEN BLUE YELLOW PINK ORANGE. Color according to the instructions. Count and write the number. Celia Rodríguez Ruiz

North Arkansas College Course Syllabus for Music Appreciation

Transcription:

61A Lecture 16 Friday, October 11

Announcements Homework 5 is due Tuesday 10/15 @ 11:59pm Project 3 is due Thursday 10/24 @ 11:59pm Midterm 2 is on Monday 10/28 7pm-9pm 2

Attributes

Terminology: Attributes, Functions, and Methods All objects have attributes, which are name-value pairs Classes are objects too, so they have attributes Instance attribute: attribute of an instance Class attribute: attribute of the class of an instance Terminology: Python object system: Functions are objects. Class Attributes Methods Functions Bound methods are also objects: a function that has its first parameter "self" already bound to an instance. Dot expressions evaluate to bound methods for class attributes that are functions. <instance>.<method_name> 4

Looking Up Attributes of an Object <expression>. <name> To evaluate a dot expression: 1.Evaluate the <expression>. 2.<name> is matched against the instance attributes. 3.If not found, <name> is looked up in the class. 4.That class attribute value is returned unless it is a function, in which case a bound method is returned. 5

Attribute Assignment

Assignment to Attributes Assignment statements with a dot expression on their left-hand side affect attributes for the object of that dot expression If the object is an instance, then assignment sets an instance attribute If the object is a class, then assignment sets a class attribute Instance Attribute Assignment : tom_account.interest = 0.08 This expression evaluates to an object But the name ( interest ) is not looked up Attribute assignment statement adds or modifies the attribute named interest of tom_account Class Attribute Assignment : Account.interest = 0.04 7

Attribute Assignment Statements Account class attributes interest: 0.02 0.04 0.05 (withdraw, deposit, init ) Instance attributes of jim_account balance: 0 holder: 'Jim' interest: 0.08 Instance attributes of tom_account balance: 0 holder: 'Tom' >>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> tom_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_account.interest = 0.08 >>> jim_account.interest 0.08 >>> tom_account.interest 0.04 >>> Account.interest = 0.05 >>> tom_account.interest 0.05 >>> jim_account.interest 0.08 8

Inheritance

Inheritance Inheritance is a method for relating classes together. A common use: Two similar classes differ in their degree of specialization. The specialized class may have the same attributes as the general class, along with some special-case behavior. class <name>(<base class>): <suite> Conceptually, the new subclass "shares" attributes with its base class. The subclass may override certain inherited attributes. Using inheritance, we implement a subclass by specifying its differences from the the base class. 10

Inheritance Example A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01 >>> ch.deposit(20) # Deposits are the same 20 >>> ch.withdraw(5) # Withdrawals incur a $1 fee 14 Most behavior is shared with the base class Account class CheckingAccount(Account): """A bank account that charges for withdrawals.""" withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw(self, amount + self.withdraw_fee) 11

Looking Up Attribute Names on Classes Base class attributes aren't copied into subclasses! To look up a name in a class. 1. If it names an attribute in the class, return the attribute value. 2. Otherwise, look up the name in the base class, if there is one. >>> ch = CheckingAccount('Tom') # Calls Account. init >>> ch.interest # Found in CheckingAccount 0.01 >>> ch.deposit(20) # Found in Account 20 >>> ch.withdraw(5) # Found in CheckingAccount 14 (Demo) 12

Object-Oriented Design

Designing for Inheritance Don't repeat yourself; use existing implementations. Attributes that have been overridden are still accessible via class objects. Look up attributes on instances whenever possible. class CheckingAccount(Account): """A bank account that charges for withdrawals.""" withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw(self, amount + self.withdraw_fee) Attribute look-up on base class Preferred to CheckingAccount.withdraw_fee to allow for specialized accounts 14

Inheritance and Composition Object-oriented programming shines when we adopt the metaphor. Inheritance is best for representing is-a relationships. E.g., a checking account is a specific type of account. So, CheckingAccount inherits from Account. Composition is best for representing has-a relationships. E.g., a bank has a collection of bank accounts it manages. So, A bank has a list of accounts as an attribute. (Demo) 15

Multiple Inheritance

Multiple Inheritance class SavingsAccount(Account): deposit_fee = 2 def deposit(self, amount): return Account.deposit(self, amount - self.deposit_fee) A class may inherit from multiple base classes in Python. CleverBank marketing executive wants: Low interest rate of 1% A $1 fee for withdrawals A $2 fee for deposits A free dollar when you open your account class AsSeenOnTVAccount(CheckingAccount, SavingsAccount): def init (self, account_holder): self.holder = account_holder self.balance = 1 # A free dollar! 17

Multiple Inheritance A class may inherit from multiple base classes in Python. class AsSeenOnTVAccount(CheckingAccount, SavingsAccount): def init (self, account_holder): self.holder = account_holder self.balance = 1 # A free dollar! Instance attribute SavingsAccount method CheckingAccount method >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 >>> such_a_deal.withdraw(5) 13 18

Resolving Ambiguous Class Attribute Names Account CheckingAccount SavingsAccount AsSeenOnTVAccount Instance attribute SavingsAccount method CheckingAccount method >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 >>> such_a_deal.withdraw(5) 13 19

Complicated Inheritance

Biological Inheritance some_guy Grandma Grandpa Grandaddy Gramammy Double Half Aunt Mom Dad Double Half Uncle some_other_guy Quadruple Double Half Cousin You Moral of the story: Inheritance can be complicated, so don't overuse it! 21