inheritance announcements attributes methods and functions
play

Inheritance Announcements Attributes Methods and Functions 4 - PowerPoint PPT Presentation

Inheritance Announcements Attributes Methods and Functions 4 Methods and Functions Python distinguishes between: 4 Methods and Functions Python distinguishes between: Functions , which we have been creating since the beginning of the


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

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

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

  4. Class Attributes 7

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

  6. Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of 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 7

  7. Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of 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 >>> tom_account = Account('Tom') 7

  8. Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of 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 >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') 7

  9. Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of 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 >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 7

  10. Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of 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 >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 The interest attribute is not part of the instance; it's part of the class! 7

  11. Class Attributes Class attributes are "shared" across all instances of a class because they are attributes of 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 >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 The interest attribute is not part of >>> jim_account.interest the instance; it's part of the class! 0.02 7

  12. Attribute Assignment

  13. Assignment to Attributes 9

  14. Assignment to Attributes Assignment statements with a dot expression on their left-hand side affect attributes for the object of that dot expression 9

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

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

  17. 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 class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ... tom_account = Account('Tom') 9

  18. 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 class Account: interest = 0.02 def __init__(self, holder): self.holder = holder self.balance = 0 ... tom_account = Account('Tom') 9

  19. 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 class Account: interest = 0.02 def __init__(self, holder): This expression self.holder = holder evaluates to an self.balance = 0 object ... tom_account = Account('Tom') 9

  20. 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 class Account: interest = 0.02 def __init__(self, holder): This expression self.holder = holder evaluates to an self.balance = 0 object ... tom_account = Account('Tom') But the name (“interest”) is not looked up 9

  21. 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 class Account: Attribute interest = 0.02 assignment def __init__(self, holder): statement adds This expression self.holder = holder or modifies the evaluates to an self.balance = 0 attribute named object ... “interest” of tom_account tom_account = Account('Tom') But the name (“interest”) is not looked up 9

  22. 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 class Account: Attribute Attribute interest = 0.02 assignment def __init__(self, holder): Assignment statement adds This expression self.holder = holder or modifies the evaluates to an self.balance = 0 attribute named object ... “interest” of tom_account tom_account = Account('Tom') But the name (“interest”) is not looked up 9

  23. 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 class Account: Attribute Attribute interest = 0.02 assignment def __init__(self, holder): Assignment statement adds This expression self.holder = holder or modifies the evaluates to an self.balance = 0 attribute named object ... “interest” of tom_account tom_account = Account('Tom') But the name (“interest”) is not looked up Class Attribute : Account.interest = 0.04 Assignment 9

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

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

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

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

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

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

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

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

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

  33. Attribute Assignment Statements Account class interest: 0.02 0.04 attributes (withdraw, deposit, __init__) balance: 0 balance: 0 Instance Instance holder: 'Tom' holder: 'Jim' attributes of attributes of jim_account tom_account >>> 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 10

  34. Attribute Assignment Statements Account class interest: 0.02 0.04 attributes (withdraw, deposit, __init__) balance: 0 balance: 0 Instance Instance holder: 'Tom' holder: 'Jim' attributes of attributes of jim_account tom_account >>> 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 10

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

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

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

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

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

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

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

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

  43. Inheritance

  44. Inheritance 12

  45. Inheritance Inheritance is a technique for relating classes together 12

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

  47. Inheritance Inheritance is a technique for relating classes together A common use: Two similar classes differ in their degree of specialization The specialized class may have the same attributes as the general class, along with some special-case behavior 12

  48. Inheritance Inheritance is a technique for relating classes together A common use: Two similar classes differ in their degree of specialization The specialized class may have the same attributes as the general class, along with some special-case behavior class <Name>(<Base Class>): <suite> 12

  49. Inheritance Inheritance is a technique for relating classes together A common use: Two similar classes differ in their degree of specialization The specialized class may have the same attributes as the general class, along with some special-case behavior class <Name>(<Base Class>): <suite> Conceptually, the new subclass inherits attributes of its base class 12

  50. Inheritance Inheritance is a technique for relating classes together A common use: Two similar classes differ in their degree of specialization The specialized class may have the same attributes as the general class, along with some special-case behavior class <Name>(<Base Class>): <suite> Conceptually, the new subclass inherits attributes of its base class The subclass may override certain inherited attributes 12

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

  52. Inheritance Example A CheckingAccount is a specialized type of Account 13

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

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

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

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

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

  58. 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): 13

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

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

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

  62. 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): 13

  63. 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) 13

  64. 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) 13

  65. 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) or return super() .withdraw( amount + self.withdraw_fee) 13

  66. 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) or return super() .withdraw( amount + self.withdraw_fee) 13

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

Recommend


More recommend