61A Lecture 16 Wednesday, October 3 Terminology: Attributes, - - PowerPoint PPT Presentation

61a lecture 16
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 16 Wednesday, October 3 Terminology: Attributes, - - PowerPoint PPT Presentation

61A Lecture 16 Wednesday, October 3 Terminology: Attributes, Functions, and Methods 2 Terminology: Attributes, Functions, and Methods All objects have attributes, which are name-value pairs 2 Terminology: Attributes, Functions, and Methods


slide-1
SLIDE 1

61A Lecture 16

Wednesday, October 3

slide-2
SLIDE 2

Terminology: Attributes, Functions, and Methods

2

slide-3
SLIDE 3

Terminology: Attributes, Functions, and Methods

All objects have attributes, which are name-value pairs

2

slide-4
SLIDE 4

Terminology: Attributes, Functions, and Methods

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

2

slide-5
SLIDE 5

Terminology: Attributes, Functions, and Methods

All objects have attributes, which are name-value pairs Classes are objects too, so they have attributes Instance attributes: attributes of instance objects

2

slide-6
SLIDE 6

Terminology: Attributes, Functions, and Methods

All objects have attributes, which are name-value pairs Classes are objects too, so they have attributes Instance attributes: attributes of instance objects Class attributes: attributes of class objects

2

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 Instance attributes: attributes of instance objects Class attributes: attributes of class objects

2

Terminology:

slide-8
SLIDE 8

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

Terminology:

slide-9
SLIDE 9

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

Terminology:

slide-10
SLIDE 10

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

Methods

Terminology:

slide-11
SLIDE 11

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

Methods

Terminology: Python object system:

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

Methods

Functions are objects. Terminology: Python object system:

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

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:

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 attributes: attributes of instance objects Class attributes: attributes of class objects

2

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:

slide-15
SLIDE 15

Looking Up Attributes by Name (Abbreviated)

3

<expression> . <name>

slide-16
SLIDE 16

Looking Up Attributes by Name (Abbreviated)

3

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

slide-17
SLIDE 17

Looking Up Attributes by Name (Abbreviated)

3

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

  • 1. Evaluate the <expression>.
slide-18
SLIDE 18

Looking Up Attributes by Name (Abbreviated)

3

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

  • 1. Evaluate the <expression>.
  • 2. <name> is matched against the instance attributes.
slide-19
SLIDE 19

Looking Up Attributes by Name (Abbreviated)

3

<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.
slide-20
SLIDE 20

Looking Up Attributes by Name (Abbreviated)

3

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

slide-21
SLIDE 21

Looking Up Attributes by Name (Abbreviated)

3

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

slide-22
SLIDE 22

Class Attributes

4

slide-23
SLIDE 23

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

slide-24
SLIDE 24

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

class Account(object): interest = 0.02 # Class attribute def __init__(self, account_holder): self.balance = 0 # Instance attribute self.holder = account_holder # Additional methods would be defined here

slide-25
SLIDE 25

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

class Account(object): interest = 0.02 # Class attribute def __init__(self, account_holder): self.balance = 0 # Instance attribute self.holder = account_holder # Additional methods would be defined here >>> tom_account = Account('Tom')

slide-26
SLIDE 26

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

class Account(object): interest = 0.02 # Class attribute def __init__(self, account_holder): self.balance = 0 # Instance attribute self.holder = account_holder # Additional methods would be defined here >>> tom_account = Account('Tom') >>> jim_account = Account('Jim')

slide-27
SLIDE 27

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

class Account(object): interest = 0.02 # Class attribute def __init__(self, account_holder): self.balance = 0 # Instance attribute self.holder = account_holder # Additional methods would be defined here >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02

slide-28
SLIDE 28

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

class Account(object): interest = 0.02 # Class attribute def __init__(self, account_holder): self.balance = 0 # Instance attribute self.holder = account_holder # Additional methods would be defined here >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02

slide-29
SLIDE 29

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.

4

class Account(object): interest = 0.02 # Class attribute def __init__(self, account_holder): self.balance = 0 # Instance attribute self.holder = account_holder # Additional methods would be defined here >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 interest is not part

  • f the instance that

was somehow copied from the class!

slide-30
SLIDE 30

Assignment to Attributes

5

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

5

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

5

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

5

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

5

tom_account.interest = 0.08

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

5

tom_account.interest = 0.08 This expression evaluates to an object

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

5

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

slide-37
SLIDE 37

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

5

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

slide-38
SLIDE 38

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

5

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

slide-39
SLIDE 39

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

5

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

slide-40
SLIDE 40

Attribute Assignment Statements

6

slide-41
SLIDE 41

Attribute Assignment Statements

6

interest: 0.02

slide-42
SLIDE 42

Attribute Assignment Statements

6

interest: 0.02 Account class attributes

slide-43
SLIDE 43

Attribute Assignment Statements

6

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

slide-44
SLIDE 44

Attribute Assignment Statements

6

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

slide-45
SLIDE 45

Attribute Assignment Statements

6

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

slide-46
SLIDE 46

Attribute Assignment Statements

6

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

slide-47
SLIDE 47

Attribute Assignment Statements

6

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

slide-48
SLIDE 48

Attribute Assignment Statements

6

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

slide-49
SLIDE 49

Attribute Assignment Statements

6

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

slide-50
SLIDE 50

Attribute Assignment Statements

6

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

slide-51
SLIDE 51

Attribute Assignment Statements

6

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

slide-52
SLIDE 52

Attribute Assignment Statements

6

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

slide-53
SLIDE 53

Attribute Assignment Statements

6

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

slide-54
SLIDE 54

Attribute Assignment Statements

6

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

slide-55
SLIDE 55

Attribute Assignment Statements

6

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

slide-56
SLIDE 56

Attribute Assignment Statements

6

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

slide-57
SLIDE 57

Attribute Assignment Statements

6

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

slide-58
SLIDE 58

Attribute Assignment Statements

6

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

slide-59
SLIDE 59

Attribute Assignment Statements

6

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

slide-60
SLIDE 60

Attribute Assignment Statements

6

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

slide-61
SLIDE 61

Attribute Assignment Statements

6

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

slide-62
SLIDE 62

Inheritance

7

slide-63
SLIDE 63

Inheritance

A technique for relating classes together

7

slide-64
SLIDE 64

Inheritance

A technique for relating classes together Common use: Similar classes differ in amount of specialization

7

slide-65
SLIDE 65

Inheritance

A technique for relating classes together Common use: Similar classes differ in amount of specialization Two classes have overlapping attribute sets, but one represents a special case of the other.

7

slide-66
SLIDE 66

Inheritance

A technique for relating classes together Common use: Similar classes differ in amount of specialization Two classes have overlapping attribute sets, but one represents a special case of the other.

7

class <name>(<base class>): <suite>

slide-67
SLIDE 67

Inheritance

A technique for relating classes together Common use: Similar classes differ in amount of specialization Two classes have overlapping attribute sets, but one represents a special case of the other.

7

class <name>(<base class>): <suite> Conceptually, the new subclass "shares" attributes with its base class.

slide-68
SLIDE 68

Inheritance

A technique for relating classes together Common use: Similar classes differ in amount of specialization Two classes have overlapping attribute sets, but one represents a special case of the other.

7

class <name>(<base class>): <suite> Conceptually, the new subclass "shares" attributes with its base class. The subclass may override certain inherited attributes.

slide-69
SLIDE 69

Inheritance

A technique for relating classes together Common use: Similar classes differ in amount of specialization Two classes have overlapping attribute sets, but one represents a special case of the other.

7

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 difference from the the base class.

slide-70
SLIDE 70

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

slide-71
SLIDE 71

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

>>> ch = CheckingAccount('Tom')

slide-72
SLIDE 72

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

>>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01

slide-73
SLIDE 73

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

>>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01 >>> ch.deposit(20) # Deposits are the same 20

slide-74
SLIDE 74

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-75
SLIDE 75

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-76
SLIDE 76

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-77
SLIDE 77

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-78
SLIDE 78

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-79
SLIDE 79

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-80
SLIDE 80

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-81
SLIDE 81

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-82
SLIDE 82

Inheritance Example

A CheckingAccount is a specialized type of Account.

8

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

slide-83
SLIDE 83

Looking Up Attribute Names on Classes

9

Base class attributes aren't copied into subclasses!

slide-84
SLIDE 84

Looking Up Attribute Names on Classes

To look up a name in a class.

9

Base class attributes aren't copied into subclasses!

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.

9

Base class attributes aren't copied into subclasses!

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.

9

Base class attributes aren't copied into subclasses!

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.

9

>>> ch = CheckingAccount('Tom') # Calls Account.__init__ Base class attributes aren't copied into subclasses!

slide-88
SLIDE 88

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.

9

>>> ch = CheckingAccount('Tom') # Calls Account.__init__ >>> ch.interest # Found in CheckingAccount 0.01 Base class attributes aren't copied into subclasses!

slide-89
SLIDE 89

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.

9

>>> 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!

slide-90
SLIDE 90

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.

9

>>> 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!

slide-91
SLIDE 91

Designing for Inheritance

10

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)

slide-92
SLIDE 92

Designing for Inheritance

Don't repeat yourself; use existing implementations.

10

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)

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.

10

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)

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.

10

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
slide-95
SLIDE 95

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.

10

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
slide-96
SLIDE 96

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.

10

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

Preferable alternative to CheckingAccount.withdraw_fee

slide-97
SLIDE 97

Designing for Inheritance: General Base Classes

11

slide-98
SLIDE 98

Designing for Inheritance: General Base Classes

11

Base classes may contain logic that is meant for subclasses.

slide-99
SLIDE 99

Designing for Inheritance: General Base Classes

11

Base classes may contain logic that is meant for subclasses. Example: Same CheckingAccount behavior; different approach Demo

slide-100
SLIDE 100

Inheritance and Composition

12

slide-101
SLIDE 101

Inheritance and Composition

Object-oriented programming shines when we adopt the metaphor.

12

slide-102
SLIDE 102

Inheritance and Composition

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

12

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.

12

slide-104
SLIDE 104

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.

12

slide-105
SLIDE 105

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.

12

slide-106
SLIDE 106

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.

12

slide-107
SLIDE 107

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 Account instances as an attribute.

12

slide-108
SLIDE 108

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 Account instances as an attribute. No local state at all? Just write a pure function!

12

slide-109
SLIDE 109

Multiple Inheritance

13

slide-110
SLIDE 110

Multiple Inheritance

13

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

slide-111
SLIDE 111

Multiple Inheritance

13

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.

slide-112
SLIDE 112

Multiple Inheritance

13

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:

slide-113
SLIDE 113

Multiple Inheritance

13

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%
slide-114
SLIDE 114

Multiple Inheritance

13

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
slide-115
SLIDE 115

Multiple Inheritance

13

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
slide-116
SLIDE 116

Multiple Inheritance

13

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
slide-117
SLIDE 117

Multiple Inheritance

13

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
slide-118
SLIDE 118

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

class AsSeenOnTVAccount(CheckingAccount, SavingsAccount): def __init__(self, account_holder): self.holder = account_holder self.balance = 1 # A free dollar!

slide-119
SLIDE 119

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

class AsSeenOnTVAccount(CheckingAccount, SavingsAccount): def __init__(self, account_holder): self.holder = account_holder self.balance = 1 # A free dollar! >>> such_a_deal = AsSeenOnTVAccount("John")

slide-120
SLIDE 120

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

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

slide-121
SLIDE 121

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

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

slide-122
SLIDE 122

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

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

slide-123
SLIDE 123

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

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

slide-124
SLIDE 124

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

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

slide-125
SLIDE 125

Multiple Inheritance

A class may inherit from multiple base classes in Python.

14

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

slide-126
SLIDE 126

Resolving Ambiguous Class Attribute Names

15

>>> 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-127
SLIDE 127

Resolving Ambiguous Class Attribute Names

15

Account CheckingAccount SavingsAccount AsSeenOnTVAccount >>> 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-128
SLIDE 128

Human Relationships

16

slide-129
SLIDE 129

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy

slide-130
SLIDE 130

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy Mom Dad

slide-131
SLIDE 131

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy Mom Dad You

slide-132
SLIDE 132

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You

slide-133
SLIDE 133

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You Some_Guy

slide-134
SLIDE 134

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You Half Some_Guy

slide-135
SLIDE 135

Human Relationships

16

Grandma Grandpa Gramammy Grandaddy Aunt Mom Dad You Half Some_Guy Half Cousin Some_Dude

slide-136
SLIDE 136

Human Relationships

16

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

slide-137
SLIDE 137

Human Relationships

16

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

slide-138
SLIDE 138

Human Relationships

16

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

slide-139
SLIDE 139

Human Relationships

16

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

slide-140
SLIDE 140

Human Relationships

16

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

slide-141
SLIDE 141

Human Relationships

16

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