61A Lecture 14 Wednesday, February 25 Announcements 2 - - PowerPoint PPT Presentation

61a lecture 14
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 14 Wednesday, February 25 Announcements 2 - - PowerPoint PPT Presentation

61A Lecture 14 Wednesday, February 25 Announcements 2 Announcements Project 2 due Thursday 2/26 @ 11:59pm 2 Announcements Project 2 due Thursday 2/26 @ 11:59pm Extra office hours on Wednesday 2/25 4pm-6pm in Bechtel (Garbarini


slide-1
SLIDE 1

61A Lecture 14

Wednesday, February 25

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Project 2 due Thursday 2/26 @ 11:59pm

2

slide-4
SLIDE 4

Announcements

  • Project 2 due Thursday 2/26 @ 11:59pm

§Extra office hours on Wednesday 2/25 4pm-6pm in Bechtel (Garbarini Lounge)

2

slide-5
SLIDE 5

Announcements

  • Project 2 due Thursday 2/26 @ 11:59pm

§Extra office hours on Wednesday 2/25 4pm-6pm in Bechtel (Garbarini Lounge) §Bonus point for early submission by Wednesday 2/25 @ 11:59pm!

2

slide-6
SLIDE 6

Announcements

  • Project 2 due Thursday 2/26 @ 11:59pm

§Extra office hours on Wednesday 2/25 4pm-6pm in Bechtel (Garbarini Lounge) §Bonus point for early submission by Wednesday 2/25 @ 11:59pm!

  • Relocated office hours on Thursday 2/26: 380 Soda (11am-3pm) & 606 Soda (3pm-6pm)

2

slide-7
SLIDE 7

Object-Oriented Programming

slide-8
SLIDE 8

Object-Oriented Programming

4

slide-9
SLIDE 9

Object-Oriented Programming

A method for organizing programs

4

slide-10
SLIDE 10

Object-Oriented Programming

A method for organizing programs

  • Data abstraction

4

slide-11
SLIDE 11

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

4

slide-12
SLIDE 12

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

4

slide-13
SLIDE 13

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state

4

slide-14
SLIDE 14

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

4

slide-15
SLIDE 15

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects

4

slide-16
SLIDE 16

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type

4

slide-17
SLIDE 17

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

4

slide-18
SLIDE 18

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

slide-19
SLIDE 19

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account

slide-20
SLIDE 20

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account Steven's Account

slide-21
SLIDE 21

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account Steven's Account John

slide-22
SLIDE 22

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account Steven's Account John Withdraw $10

slide-23
SLIDE 23

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account Steven's Account John Withdraw $10 Deposit $10

slide-24
SLIDE 24

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account Steven's Account John Withdraw $10 Deposit $10

slide-25
SLIDE 25

Object-Oriented Programming

A method for organizing programs

  • Data abstraction
  • Bundling together information and related behavior

A metaphor for computation using distributed state

  • Each object has its own local state
  • Each object also knows how to manage its own local state,

based on method calls

  • Method calls are messages passed between objects
  • Several objects may all be instances of a common type
  • Different types may relate to each other

Specialized syntax & vocabulary to support this metaphor

4

John's Account Steven's Account John Withdraw $10 Deposit $10 Apply for a loan!

slide-26
SLIDE 26

Classes

5

slide-27
SLIDE 27

Classes

A class serves as a template for its instances.

5

slide-28
SLIDE 28

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance.

5

slide-29
SLIDE 29

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. >>> a = Account('Jim')

5

slide-30
SLIDE 30

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. >>> a = Account('Jim')

5

slide-31
SLIDE 31

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. >>> a = Account('Jim')

5

slide-32
SLIDE 32

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. >>> a = Account('Jim') >>> a.holder 'Jim'

5

slide-33
SLIDE 33

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance

5

slide-34
SLIDE 34

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. Idea: All bank accounts should have "withdraw" and "deposit" behaviors that all work in the same way. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance

5

slide-35
SLIDE 35

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. Idea: All bank accounts should have "withdraw" and "deposit" behaviors that all work in the same way. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance >>> a.deposit(15) 15

5

slide-36
SLIDE 36

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. Idea: All bank accounts should have "withdraw" and "deposit" behaviors that all work in the same way. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance >>> a.deposit(15) 15 >>> a.withdraw(10) 5

5

slide-37
SLIDE 37

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. Idea: All bank accounts should have "withdraw" and "deposit" behaviors that all work in the same way. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance >>> a.deposit(15) 15 >>> a.withdraw(10) 5 >>> a.balance 5

5

slide-38
SLIDE 38

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. Idea: All bank accounts should have "withdraw" and "deposit" behaviors that all work in the same way. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance >>> a.deposit(15) 15 >>> a.withdraw(10) 5 >>> a.balance 5 >>> a.withdraw(10) 'Insufficient funds'

5

slide-39
SLIDE 39

Classes

A class serves as a template for its instances. Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes to each newly created instance. Idea: All bank accounts should have "withdraw" and "deposit" behaviors that all work in the same way. >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance >>> a.deposit(15) 15 >>> a.withdraw(10) 5 >>> a.balance 5 >>> a.withdraw(10) 'Insufficient funds' Better idea: All bank accounts share a "withdraw" method and a "deposit" method.

5

slide-40
SLIDE 40

Class Statements

slide-41
SLIDE 41

The Class Statement

7

slide-42
SLIDE 42

The Class Statement

7

class <name>: <suite>

slide-43
SLIDE 43

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment.

7

class <name>: <suite>

slide-44
SLIDE 44

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment. Assignment & def statements in <suite> create attributes of the class (not names in frames)

7

class <name>: <suite>

slide-45
SLIDE 45

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment. Assignment & def statements in <suite> create attributes of the class (not names in frames)

7

The suite is executed when the class statement is executed. class <name>: <suite>

slide-46
SLIDE 46

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment. Assignment & def statements in <suite> create attributes of the class (not names in frames)

7

The suite is executed when the class statement is executed. >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... class <name>: <suite>

slide-47
SLIDE 47

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment. Assignment & def statements in <suite> create attributes of the class (not names in frames)

7

The suite is executed when the class statement is executed. >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' class <name>: <suite>

slide-48
SLIDE 48

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment. Assignment & def statements in <suite> create attributes of the class (not names in frames)

7

The suite is executed when the class statement is executed. >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' >>> Clown.dance() 'No thanks' class <name>: <suite>

slide-49
SLIDE 49

The Class Statement

A class statement creates a new class and binds that class to <name> in the first frame of the current environment. Assignment & def statements in <suite> create attributes of the class (not names in frames)

7

The suite is executed when the class statement is executed. >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' >>> Clown.dance() 'No thanks' >>> Clown <class '__main__.Clown'> class <name>: <suite>

slide-50
SLIDE 50

Object Construction

8

slide-51
SLIDE 51

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim')

8

slide-52
SLIDE 52

When a class is called:

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim')

8

slide-53
SLIDE 53

When a class is called: 1.A new instance of that class is created:

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim')

8

slide-54
SLIDE 54

When a class is called: 1.A new instance of that class is created:

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim')

8

slide-55
SLIDE 55

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim')

8

slide-56
SLIDE 56

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

slide-57
SLIDE 57

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

slide-58
SLIDE 58

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

slide-59
SLIDE 59

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

balance: 0

slide-60
SLIDE 60

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

balance: 0 holder: 'Jim'

slide-61
SLIDE 61

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

balance: 0 holder: 'Jim' __init__ is called a constructor

slide-62
SLIDE 62

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') >>> a.holder 'Jim' class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

balance: 0 holder: 'Jim' __init__ is called a constructor

slide-63
SLIDE 63

When a class is called: 1.A new instance of that class is created: 2.The __init__ method of the class is called with the new object as its first argument (named self), along with any additional arguments provided in the call expression.

An account instance

Object Construction

Idea: All bank accounts have a balance and an account holder; 
 the Account class should add those attributes to each of its instances >>> a = Account('Jim') >>> a.holder 'Jim' >>> a.balance class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

8

balance: 0 holder: 'Jim' __init__ is called a constructor

slide-64
SLIDE 64

Object Identity

9

slide-65
SLIDE 65

Object Identity

Every object that is an instance of a user-defined class has a unique identity:

9

slide-66
SLIDE 66

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') Every object that is an instance of a user-defined class has a unique identity:

9

slide-67
SLIDE 67

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') Every object that is an instance of a user-defined class has a unique identity:

9

Every call to Account creates a new Account

  • instance. There is only one Account class.
slide-68
SLIDE 68

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') Every object that is an instance of a user-defined class has a unique identity:

9

Every call to Account creates a new Account

  • instance. There is only one Account class.

>>> a.balance >>> b.holder 'Jack'

slide-69
SLIDE 69

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') Every object that is an instance of a user-defined class has a unique identity: Identity operators "is" and "is not" test if two expressions evaluate to the same object:

9

Every call to Account creates a new Account

  • instance. There is only one Account class.

>>> a.balance >>> b.holder 'Jack'

slide-70
SLIDE 70

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') >>> a is a True >>> a is not b True Every object that is an instance of a user-defined class has a unique identity: Identity operators "is" and "is not" test if two expressions evaluate to the same object:

9

Every call to Account creates a new Account

  • instance. There is only one Account class.

>>> a.balance >>> b.holder 'Jack'

slide-71
SLIDE 71

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') >>> a is a True >>> a is not b True Every object that is an instance of a user-defined class has a unique identity: Binding an object to a new name using assignment does not create a new object: Identity operators "is" and "is not" test if two expressions evaluate to the same object:

9

Every call to Account creates a new Account

  • instance. There is only one Account class.

>>> a.balance >>> b.holder 'Jack'

slide-72
SLIDE 72

Object Identity

>>> a = Account('Jim') >>> b = Account('Jack') >>> a is a True >>> a is not b True Every object that is an instance of a user-defined class has a unique identity: Binding an object to a new name using assignment does not create a new object: Identity operators "is" and "is not" test if two expressions evaluate to the same object: >>> c = a >>> c is a True

9

Every call to Account creates a new Account

  • instance. There is only one Account class.

>>> a.balance >>> b.holder 'Jack'

slide-73
SLIDE 73

Methods

slide-74
SLIDE 74

Methods

11

slide-75
SLIDE 75

Methods

Methods are functions defined in the suite of a class statement

11

slide-76
SLIDE 76

Methods

Methods are functions defined in the suite of a class statement class Account:

11

slide-77
SLIDE 77

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder):

11

slide-78
SLIDE 78

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0

11

slide-79
SLIDE 79

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder

11

slide-80
SLIDE 80

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount):

11

slide-81
SLIDE 81

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount):

11

self should always be bound to an instance of the Account class

slide-82
SLIDE 82

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount

11

self should always be bound to an instance of the Account class

slide-83
SLIDE 83

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance

11

self should always be bound to an instance of the Account class

slide-84
SLIDE 84

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount):

11

self should always be bound to an instance of the Account class

slide-85
SLIDE 85

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance:

11

self should always be bound to an instance of the Account class

slide-86
SLIDE 86

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds'

11

self should always be bound to an instance of the Account class

slide-87
SLIDE 87

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds' self.balance = self.balance - amount

11

self should always be bound to an instance of the Account class

slide-88
SLIDE 88

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds' self.balance = self.balance - amount return self.balance

11

self should always be bound to an instance of the Account class

slide-89
SLIDE 89

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds' self.balance = self.balance - amount return self.balance These def statements create function objects as always,
 but their names are bound as attributes of the class

11

self should always be bound to an instance of the Account class

slide-90
SLIDE 90

Methods

Methods are functions defined in the suite of a class statement class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds' self.balance = self.balance - amount return self.balance These def statements create function objects as always,
 but their names are bound as attributes of the class

11

self should always be bound to an instance of the Account class s

slide-91
SLIDE 91

Invoking Methods

12

slide-92
SLIDE 92

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state.

12

slide-93
SLIDE 93

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state. class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance

12

slide-94
SLIDE 94

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state. class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance Defined with two parameters

12

slide-95
SLIDE 95

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state. class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance Dot notation automatically supplies the first argument to a method. Defined with two parameters

12

slide-96
SLIDE 96

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state. class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Dot notation automatically supplies the first argument to a method. Defined with two parameters

12

slide-97
SLIDE 97

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state. class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Dot notation automatically supplies the first argument to a method. Invoked with one argument Defined with two parameters

12

slide-98
SLIDE 98

Invoking Methods

All invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state. class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Dot notation automatically supplies the first argument to a method. Invoked with one argument Defined with two parameters

12

Bound to self

slide-99
SLIDE 99

Dot Expressions

13

slide-100
SLIDE 100

Dot Expressions

Objects receive messages via dot notation.

13

slide-101
SLIDE 101

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class.

13

slide-102
SLIDE 102

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name>

13

slide-103
SLIDE 103

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression.

13

slide-104
SLIDE 104

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression. The <name> must be a simple name.

13

slide-105
SLIDE 105

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression. The <name> must be a simple name. Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression>.

13

slide-106
SLIDE 106

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression. The <name> must be a simple name. Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression>. tom_account.deposit(10)

13

slide-107
SLIDE 107

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression. The <name> must be a simple name. Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression>. tom_account.deposit(10) Dot expression

13

slide-108
SLIDE 108

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression. The <name> must be a simple name. Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression>. tom_account.deposit(10) Dot expression Call expression

13

slide-109
SLIDE 109

Dot Expressions

Objects receive messages via dot notation. Dot notation accesses attributes of the instance or its class. <expression> . <name> The <expression> can be any valid Python expression. The <name> must be a simple name. Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression>. tom_account.deposit(10) Dot expression Call expression

13

(Demo)

slide-110
SLIDE 110

Attributes

(Demo)

slide-111
SLIDE 111

Accessing Attributes

15

slide-112
SLIDE 112

Accessing Attributes

Using getattr, we can look up an attribute using a string

15

slide-113
SLIDE 113

Accessing Attributes

Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10

15

slide-114
SLIDE 114

Accessing Attributes

Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True

15

slide-115
SLIDE 115

Accessing Attributes

Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True getattr and dot expressions look up a name in the same way

15

slide-116
SLIDE 116

Accessing Attributes

Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True getattr and dot expressions look up a name in the same way Looking up an attribute name in an object may return:

15

slide-117
SLIDE 117

Accessing Attributes

Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True getattr and dot expressions look up a name in the same way Looking up an attribute name in an object may return:

  • One of its instance attributes, or

15

slide-118
SLIDE 118

Accessing Attributes

Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True getattr and dot expressions look up a name in the same way Looking up an attribute name in an object may return:

  • One of its instance attributes, or
  • One of the attributes of its class

15

slide-119
SLIDE 119

Methods and Functions

16

slide-120
SLIDE 120

Methods and Functions

Python distinguishes between:

16

slide-121
SLIDE 121

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and

16

slide-122
SLIDE 122

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked.

16

slide-123
SLIDE 123

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method

16

slide-124
SLIDE 124

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit)

16

slide-125
SLIDE 125

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'>

16

slide-126
SLIDE 126

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit)

16

slide-127
SLIDE 127

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'>

16

slide-128
SLIDE 128

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'> >>> Account.deposit(tom_account, 1001) 1011

16

slide-129
SLIDE 129

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'> >>> Account.deposit(tom_account, 1001) 1011 >>> tom_account.deposit(1003) 2014

16

slide-130
SLIDE 130

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'> >>> Account.deposit(tom_account, 1001) 1011 >>> tom_account.deposit(1003) 2014

16

Function: all arguments within parentheses

slide-131
SLIDE 131

Methods and Functions

Python distinguishes between:

  • Functions, which we have been creating since the beginning of the course, and
  • Bound methods, which couple together a function and the object on which that

method will be invoked. Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'> >>> Account.deposit(tom_account, 1001) 1011 >>> tom_account.deposit(1003) 2014

16

Function: all arguments within parentheses Method: One object before the dot and

  • ther arguments within parentheses
slide-132
SLIDE 132

Looking Up Attributes by Name

<expression> . <name>

17

slide-133
SLIDE 133

Looking Up Attributes by Name

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

17

slide-134
SLIDE 134

Looking Up Attributes by Name

<expression> . <name> To evaluate a dot expression: 1. Evaluate the <expression> to the left of the dot, which yields the object of the dot expression.

17

slide-135
SLIDE 135

Looking Up Attributes by Name

<expression> . <name> To evaluate a dot expression: 1. Evaluate the <expression> to the left of the dot, which yields the object of the dot expression. 2. <name> is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned.

17

slide-136
SLIDE 136

Looking Up Attributes by Name

<expression> . <name> To evaluate a dot expression: 1. Evaluate the <expression> to the left of the dot, which yields the object of the dot expression. 2. <name> is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned. 3. If not, <name> is looked up in the class, which yields a class attribute value.

17

slide-137
SLIDE 137

Looking Up Attributes by Name

<expression> . <name> To evaluate a dot expression: 1. Evaluate the <expression> to the left of the dot, which yields the object of the dot expression. 2. <name> is matched against the instance attributes of that object; if an attribute with that name exists, its value is returned. 3. If not, <name> is looked up in the class, which yields a class attribute value. 4. That value is returned unless it is a function, in which case a bound method is returned instead.

17

slide-138
SLIDE 138

Class Attributes

18

slide-139
SLIDE 139

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

18

slide-140
SLIDE 140

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

class Account:

  • interest = 0.02 # A class attribute
  • def __init__(self, account_holder):

self.balance = 0 self.holder = account_holder

  • # Additional methods would be defined here

18

slide-141
SLIDE 141

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

class Account:

  • interest = 0.02 # A class attribute
  • def __init__(self, account_holder):

self.balance = 0 self.holder = account_holder

  • # Additional methods would be defined here

18

>>> tom_account = Account('Tom')

slide-142
SLIDE 142

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

class Account:

  • interest = 0.02 # A class attribute
  • def __init__(self, account_holder):

self.balance = 0 self.holder = account_holder

  • # Additional methods would be defined here

18

>>> tom_account = Account('Tom') >>> jim_account = Account('Jim')

slide-143
SLIDE 143

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

class Account:

  • interest = 0.02 # A class attribute
  • def __init__(self, account_holder):

self.balance = 0 self.holder = account_holder

  • # Additional methods would be defined here

18

>>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02

slide-144
SLIDE 144

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

class Account:

  • interest = 0.02 # A class attribute
  • def __init__(self, account_holder):

self.balance = 0 self.holder = account_holder

  • # Additional methods would be defined here

The interest attribute is not part of the instance; it's part of the class!

18

>>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02

slide-145
SLIDE 145

Class Attributes

Class attributes are "shared" across all instances of a class because they are attributes

  • f the class, not the instance.

class Account:

  • interest = 0.02 # A class attribute
  • def __init__(self, account_holder):

self.balance = 0 self.holder = account_holder

  • # Additional methods would be defined here

The interest attribute is not part of the instance; it's part of the class!

18

>>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02

slide-146
SLIDE 146

Attribute Assignment

slide-147
SLIDE 147

Assignment to Attributes

20

slide-148
SLIDE 148

Assignment to Attributes

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

20

slide-149
SLIDE 149

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

20

slide-150
SLIDE 150

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

20

slide-151
SLIDE 151

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

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-152
SLIDE 152

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

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-153
SLIDE 153

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

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-154
SLIDE 154

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

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-155
SLIDE 155

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

  • r modifies the

attribute named “interest” of tom_account This expression evaluates to an

  • bject

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-156
SLIDE 156

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

  • r modifies the

attribute named “interest” of tom_account Instance Attribute Assignment : This expression evaluates to an

  • bject

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-157
SLIDE 157

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

  • r modifies the

attribute named “interest” of tom_account Instance Attribute Assignment : Account.interest = 0.04 Class Attribute Assignment : This expression evaluates to an

  • bject

20

class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ...

  • tom_account = Account('Tom')
slide-158
SLIDE 158

Attribute Assignment Statements

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

21

slide-159
SLIDE 159

Attribute Assignment Statements

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

21

slide-160
SLIDE 160

Attribute Assignment Statements

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

21

Instance attributes of jim_account

slide-161
SLIDE 161

Attribute Assignment Statements

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

21

Instance attributes of jim_account

slide-162
SLIDE 162

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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-163
SLIDE 163

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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-164
SLIDE 164

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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-165
SLIDE 165

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-166
SLIDE 166

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-167
SLIDE 167

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-168
SLIDE 168

Attribute Assignment Statements

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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-169
SLIDE 169

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-170
SLIDE 170

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-171
SLIDE 171

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-172
SLIDE 172

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-173
SLIDE 173

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-174
SLIDE 174

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-175
SLIDE 175

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account

slide-176
SLIDE 176

Attribute Assignment Statements

>>> jim_account = Account('Jim') >>> tom_account = Account('Tom') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02 >>> Account.interest = 0.04 >>> tom_account.interest 0.04 >>> jim_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

21

Instance attributes of jim_account Instance attributes of tom_account