61A Lecture 16 Friday, October 11 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 16
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 16 Friday, October 11 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 16 Friday, October 11 Announcements 2 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,


slide-1
SLIDE 1

61A Lecture 16

Friday, October 11

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Attributes

slide-5
SLIDE 5

Terminology: Attributes, Functions, and Methods

4

slide-6
SLIDE 6

Terminology: Attributes, Functions, and Methods

All objects have attributes, which are name-value pairs

4

slide-7
SLIDE 7

Terminology: Attributes, Functions, and Methods

All objects have attributes, which are name-value pairs Classes are objects too, so they have attributes

4

slide-8
SLIDE 8

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

4

slide-9
SLIDE 9

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

4

slide-10
SLIDE 10

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:

4

slide-11
SLIDE 11

Class 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:

4

slide-12
SLIDE 12

Class Attributes Functions

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:

4

slide-13
SLIDE 13

Class Attributes Functions

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

Methods

Terminology:

4

slide-14
SLIDE 14

Class Attributes Functions

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

Methods

Terminology: Python object system:

4

slide-15
SLIDE 15

Class Attributes Functions

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

Methods

Functions are objects. Terminology: Python object system:

4

slide-16
SLIDE 16

Class Attributes Functions

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

Methods

Functions are objects. Bound methods are also objects: a function that has its first parameter "self" already bound to an instance. Terminology: Python object system:

4

slide-17
SLIDE 17

Class Attributes Functions

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

Methods

Functions are objects. 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. Terminology: Python object system:

4

slide-18
SLIDE 18

Class Attributes Functions

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

Methods

Functions are objects. 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. Terminology: Python object system:

4

<instance>.<method_name>

slide-19
SLIDE 19

Looking Up Attributes of an Object

<expression> . <name>

5

slide-20
SLIDE 20

Looking Up Attributes of an Object

<expression> . <name> To evaluate a dot expression:

5

slide-21
SLIDE 21

Looking Up Attributes of an Object

<expression> . <name> To evaluate a dot expression: 1.Evaluate the <expression>.

5

slide-22
SLIDE 22

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.

5

slide-23
SLIDE 23

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.

5

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

slide-26
SLIDE 26

Attribute Assignment

slide-27
SLIDE 27

Assignment to Attributes

7

slide-28
SLIDE 28

Assignment to Attributes

Assignment statements with a dot expression on their left-hand side affect attributes for the object of that dot expression

7

slide-29
SLIDE 29

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

7

slide-30
SLIDE 30

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

7

slide-31
SLIDE 31

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

tom_account.interest = 0.08

7

slide-32
SLIDE 32

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

tom_account.interest = 0.08 This expression evaluates to an

  • bject

7

slide-33
SLIDE 33

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

tom_account.interest = 0.08 But the name (“interest”) is not looked up This expression evaluates to an

  • bject

7

slide-34
SLIDE 34

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

tom_account.interest = 0.08 But the name (“interest”) is not looked up Attribute assignment statement adds or modifies the attribute named “interest” of tom_account This expression evaluates to an

  • bject

7

slide-35
SLIDE 35

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

tom_account.interest = 0.08 But the name (“interest”) is not looked up Attribute assignment statement adds or modifies the attribute named “interest” of tom_account Instance Attribute Assignment : This expression evaluates to an

  • bject

7

slide-36
SLIDE 36

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

tom_account.interest = 0.08 But the name (“interest”) is not looked up Attribute assignment statement adds or modifies the attribute named “interest” of tom_account Instance Attribute Assignment : Account.interest = 0.04 Class Attribute Assignment : This expression evaluates to an

  • bject

7

slide-37
SLIDE 37

Attribute Assignment Statements

interest: 0.02 (withdraw, deposit, __init__) Account class attributes

8

slide-38
SLIDE 38

Attribute Assignment Statements

>>> jim_account = Account('Jim') interest: 0.02 (withdraw, deposit, __init__) Account class attributes

8

slide-39
SLIDE 39

Attribute Assignment Statements

>>> jim_account = Account('Jim') interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' Account class attributes

8

slide-40
SLIDE 40

Attribute Assignment Statements

>>> jim_account = Account('Jim') interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' Account class attributes

8

Instance attributes of jim_account

slide-41
SLIDE 41

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' Account class attributes

8

Instance attributes of jim_account

slide-42
SLIDE 42

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes

8

Instance attributes of jim_account Instance attributes of tom_account

slide-43
SLIDE 43

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes

8

Instance attributes of jim_account Instance attributes of tom_account

slide-44
SLIDE 44

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes

8

Instance attributes of jim_account Instance attributes of tom_account

slide-45
SLIDE 45

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> tom_account.interest 0.02 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes

8

Instance attributes of jim_account Instance attributes of tom_account

slide-46
SLIDE 46

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes

8

Instance attributes of jim_account Instance attributes of tom_account

slide-47
SLIDE 47

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04

8

Instance attributes of jim_account Instance attributes of tom_account

slide-48
SLIDE 48

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04

8

Instance attributes of jim_account Instance attributes of tom_account

slide-49
SLIDE 49

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04

8

Instance attributes of jim_account Instance attributes of tom_account

slide-50
SLIDE 50

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08

8

Instance attributes of jim_account Instance attributes of tom_account

slide-51
SLIDE 51

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08

8

Instance attributes of jim_account Instance attributes of tom_account

slide-52
SLIDE 52

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08

8

Instance attributes of jim_account Instance attributes of tom_account

slide-53
SLIDE 53

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08

8

Instance attributes of jim_account Instance attributes of tom_account

slide-54
SLIDE 54

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08 0.05

8

Instance attributes of jim_account Instance attributes of tom_account

slide-55
SLIDE 55

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08 0.05

8

Instance attributes of jim_account Instance attributes of tom_account

slide-56
SLIDE 56

Attribute Assignment Statements

>>> 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 interest: 0.02 (withdraw, deposit, __init__) balance: 0 holder: 'Jim' balance: 0 holder: 'Tom' Account class attributes 0.04 interest: 0.08 0.05

8

Instance attributes of jim_account Instance attributes of tom_account

slide-57
SLIDE 57

Inheritance

slide-58
SLIDE 58

Inheritance

10

slide-59
SLIDE 59

Inheritance

Inheritance is a method for relating classes together.

10

slide-60
SLIDE 60

Inheritance

Inheritance is a method for relating classes together. A common use: Two similar classes differ in their degree of specialization.

10

slide-61
SLIDE 61

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.

10

slide-62
SLIDE 62

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>

10

slide-63
SLIDE 63

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.

10

slide-64
SLIDE 64

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.

10

slide-65
SLIDE 65

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

slide-66
SLIDE 66

Inheritance Example

A CheckingAccount is a specialized type of Account.

11

slide-67
SLIDE 67

Inheritance Example

A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom')

11

slide-68
SLIDE 68

Inheritance Example

A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01

11

slide-69
SLIDE 69

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

11

slide-70
SLIDE 70

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

11

slide-71
SLIDE 71

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

11

slide-72
SLIDE 72

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):

11

slide-73
SLIDE 73

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."""

11

slide-74
SLIDE 74

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

11

slide-75
SLIDE 75

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

11

slide-76
SLIDE 76

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):

11

slide-77
SLIDE 77

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

slide-78
SLIDE 78

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

slide-79
SLIDE 79

Looking Up Attribute Names on Classes

Base class attributes aren't copied into subclasses!

12

slide-80
SLIDE 80

Looking Up Attribute Names on Classes

To look up a name in a class. Base class attributes aren't copied into subclasses!

12

slide-81
SLIDE 81

Looking Up Attribute Names on Classes

To look up a name in a class.

  • 1. If it names an attribute in the class, return the attribute value.

Base class attributes aren't copied into subclasses!

12

slide-82
SLIDE 82

Looking Up Attribute Names on Classes

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.

Base class attributes aren't copied into subclasses!

12

slide-83
SLIDE 83

Looking Up Attribute Names on Classes

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__ Base class attributes aren't copied into subclasses!

12

slide-84
SLIDE 84

Looking Up Attribute Names on Classes

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 Base class attributes aren't copied into subclasses!

12

slide-85
SLIDE 85

Looking Up Attribute Names on Classes

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 Base class attributes aren't copied into subclasses!

12

slide-86
SLIDE 86

Looking Up Attribute Names on Classes

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 Base class attributes aren't copied into subclasses!

12

slide-87
SLIDE 87

Looking Up Attribute Names on Classes

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 Base class attributes aren't copied into subclasses!

12

(Demo)

slide-88
SLIDE 88

Object-Oriented Design

slide-89
SLIDE 89

Designing for Inheritance

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)

14

slide-90
SLIDE 90

Designing for Inheritance

Don't repeat yourself; use existing implementations. 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)

14

slide-91
SLIDE 91

Designing for Inheritance

Don't repeat yourself; use existing implementations. Attributes that have been overridden are still accessible via class objects. 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)

14

slide-92
SLIDE 92

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)

14

slide-93
SLIDE 93

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

  • n base class

14

slide-94
SLIDE 94

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

  • n base class

Preferred to CheckingAccount.withdraw_fee to allow for specialized accounts

14

slide-95
SLIDE 95

Inheritance and Composition

15

slide-96
SLIDE 96

Inheritance and Composition

Object-oriented programming shines when we adopt the metaphor.

15

slide-97
SLIDE 97

Inheritance and Composition

Object-oriented programming shines when we adopt the metaphor. Inheritance is best for representing is-a relationships.

15

slide-98
SLIDE 98

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.

15

slide-99
SLIDE 99

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.

15

slide-100
SLIDE 100

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.

15

slide-101
SLIDE 101

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.

15

slide-102
SLIDE 102

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.

15

slide-103
SLIDE 103

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.

15

(Demo)

slide-104
SLIDE 104

Multiple Inheritance

slide-105
SLIDE 105

Multiple Inheritance

17

slide-106
SLIDE 106

Multiple Inheritance

class SavingsAccount(Account): deposit_fee = 2 def deposit(self, amount): return Account.deposit(self, amount - self.deposit_fee)

17

slide-107
SLIDE 107

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.

17

slide-108
SLIDE 108

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:

17

slide-109
SLIDE 109

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%

17

slide-110
SLIDE 110

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

17

slide-111
SLIDE 111

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

17

slide-112
SLIDE 112

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

17

slide-113
SLIDE 113

Multiple Inheritance

class SavingsAccount(Account): deposit_fee = 2 def deposit(self, amount): return Account.deposit(self, amount - self.deposit_fee) class AsSeenOnTVAccount(CheckingAccount, SavingsAccount): def __init__(self, account_holder): self.holder = account_holder self.balance = 1 # A free dollar! 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

17

slide-114
SLIDE 114

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!

18

slide-115
SLIDE 115

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! >>> such_a_deal = AsSeenOnTVAccount("John")

18

slide-116
SLIDE 116

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! >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1

18

slide-117
SLIDE 117

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! >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 Instance attribute

18

slide-118
SLIDE 118

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! >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 Instance attribute

18

slide-119
SLIDE 119

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! >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 Instance attribute SavingsAccount method

18

slide-120
SLIDE 120

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! >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 >>> such_a_deal.withdraw(5) 13 Instance attribute SavingsAccount method

18

slide-121
SLIDE 121

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! >>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 >>> such_a_deal.withdraw(5) 13 Instance attribute SavingsAccount method CheckingAccount method

18

slide-122
SLIDE 122

Resolving Ambiguous Class Attribute Names

19

slide-123
SLIDE 123

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

slide-124
SLIDE 124

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John")

slide-125
SLIDE 125

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1

slide-126
SLIDE 126

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 Instance attribute

slide-127
SLIDE 127

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 Instance attribute

slide-128
SLIDE 128

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 Instance attribute SavingsAccount method

slide-129
SLIDE 129

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 >>> such_a_deal.withdraw(5) 13 Instance attribute SavingsAccount method

slide-130
SLIDE 130

Resolving Ambiguous Class Attribute Names

Account CheckingAccount SavingsAccount AsSeenOnTVAccount

19

>>> such_a_deal = AsSeenOnTVAccount("John") >>> such_a_deal.balance 1 >>> such_a_deal.deposit(20) 19 >>> such_a_deal.withdraw(5) 13 Instance attribute SavingsAccount method CheckingAccount method

slide-131
SLIDE 131

Complicated Inheritance

slide-132
SLIDE 132

Biological Inheritance

21

slide-133
SLIDE 133

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy

slide-134
SLIDE 134

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Mom Dad

slide-135
SLIDE 135

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Mom Dad You

slide-136
SLIDE 136

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You

slide-137
SLIDE 137

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You Half some_guy

slide-138
SLIDE 138

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You Half some_guy Half Cousin some_other_guy

slide-139
SLIDE 139

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You Half Half Cousin some_other_guy

slide-140
SLIDE 140

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Double Mom Dad You Half Half Cousin some_other_guy

slide-141
SLIDE 141

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Double Mom Dad You Half Half Cousin some_other_guy Double

slide-142
SLIDE 142

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Double Mom Dad You Half Double Half Uncle Half Cousin some_other_guy Double

slide-143
SLIDE 143

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Double Mom Dad You Half Double Half Uncle Half Cousin Double

slide-144
SLIDE 144

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Double Quadruple Mom Dad You Half Double Half Uncle Half Cousin

slide-145
SLIDE 145

Biological Inheritance

21

Grandma Grandpa Gramammy Grandaddy Aunt Double Quadruple Mom Dad You Half Double Half Uncle Half Cousin

Moral of the story: Inheritance can be complicated, so don't overuse it!