61a lecture 16
play

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


  1. 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 object 5

  2. 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 object But the name (“interest”) is not looked up 5

  3. 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 Attribute assignment statement This expression adds or evaluates to an object modifies the “interest” attribute of But the name (“interest”) tom_account is not looked up 5

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

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

  6. Attribute Assignment Statements 6

  7. Attribute Assignment Statements interest: 0.02 6

  8. Attribute Assignment Statements interest: 0.02 Account class attributes 6

  9. Attribute Assignment Statements interest: 0.02 Account (withdraw, deposit, __init__) class attributes 6

  10. Attribute Assignment Statements interest: 0.02 Account (withdraw, deposit, __init__) class attributes >>> jim_account = Account('Jim') 6

  11. Attribute Assignment Statements interest: 0.02 Account (withdraw, deposit, __init__) class attributes balance: 0 holder: 'Jim' >>> jim_account = Account('Jim') 6

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

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

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

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

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

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

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

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

  20. Attribute Assignment Statements interest: 0.02 0.04 Account (withdraw, deposit, __init__) class attributes balance: 0 balance: 0 holder: 'Jim' holder: 'Tom' >>> jim_account.interest = 0.08 >>> 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 6

  21. Attribute Assignment Statements interest: 0.02 0.04 Account (withdraw, deposit, __init__) class attributes balance: 0 balance: 0 holder: 'Jim' holder: 'Tom' interest: 0.08 >>> jim_account.interest = 0.08 >>> 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 6

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

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

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

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

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

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

  28. Inheritance 7

  29. Inheritance A technique for relating classes together 7

  30. Inheritance A technique for relating classes together Common use: Similar classes differ in amount of specialization 7

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

  32. 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. class <name>(<base class>): <suite> 7

  33. 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. class <name>(<base class>): <suite> Conceptually, the new subclass "shares" attributes with its base class. 7

  34. 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. class <name>(<base class>): <suite> Conceptually, the new subclass "shares" attributes with its base class. The subclass may override certain inherited attributes. 7

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

  36. Inheritance Example A CheckingAccount is a specialized type of Account. 8

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

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

  39. Inheritance Example A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01 >>> ch.deposit(20) # Deposits are the same 20 8

  40. Inheritance Example A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01 >>> ch.deposit(20) # Deposits are the same 20 >>> ch.withdraw(5) # Withdrawals incur a $1 fee 14 8

  41. Inheritance Example A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01 >>> ch.deposit(20) # Deposits are the same 20 >>> ch.withdraw(5) # Withdrawals incur a $1 fee 14 Most behavior is shared with the base class Account 8

  42. Inheritance Example A CheckingAccount is a specialized type of Account. >>> ch = CheckingAccount('Tom') >>> ch.interest # Lower interest rate for checking accounts 0.01 >>> ch.deposit(20) # Deposits are the same 20 >>> ch.withdraw(5) # Withdrawals incur a $1 fee 14 Most behavior is shared with the base class Account class CheckingAccount(Account): 8

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

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

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

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

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

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

  49. Looking Up Attribute Names on Classes Base class attributes aren't copied into subclasses! 9

  50. Looking Up Attribute Names on Classes Base class attributes aren't copied into subclasses! To look up a name in a class. 9

  51. Looking Up Attribute Names on Classes Base class attributes aren't copied into subclasses! To look up a name in a class. 1. If it names an attribute in the class, return the attribute value. 9

  52. Looking Up Attribute Names on Classes Base class attributes aren't copied into subclasses! To look up a name in a class. 1. If it names an attribute in the class, return the attribute value. 2. Otherwise, look up the name in the base class, if there is one. 9

  53. Looking Up Attribute Names on Classes Base class attributes aren't copied into subclasses! To look up a name in a class. 1. If it names an attribute in the class, return the attribute value. 2. Otherwise, look up the name in the base class, if there is one. >>> ch = CheckingAccount('Tom') # Calls Account.__init__ 9

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

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

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

  57. Designing for Inheritance class CheckingAccount(Account): """A bank account that charges for withdrawals.""" withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw(self, amount + self.withdraw_fee) 10

  58. Designing for Inheritance Don't repeat yourself; use existing implementations. class CheckingAccount(Account): """A bank account that charges for withdrawals.""" withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw(self, amount + self.withdraw_fee) 10

  59. Designing for Inheritance Don't repeat yourself; use existing implementations. Attributes that have been overridden are still accessible via class objects. class CheckingAccount(Account): """A bank account that charges for withdrawals.""" withdraw_fee = 1 interest = 0.01 def withdraw(self, amount): return Account.withdraw(self, amount + self.withdraw_fee) 10

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

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

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

  63. Designing for Inheritance: General Base Classes 11

  64. Designing for Inheritance: General Base Classes Base classes may contain logic that is meant for subclasses. 11

  65. Designing for Inheritance: General Base Classes Base classes may contain logic that is meant for subclasses. Example: Same CheckingAccount behavior; different approach Demo 11

  66. Inheritance and Composition 12

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend