61a lecture 14
play

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


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

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

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

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

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

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

  7. Class Statements

  8. The Class Statement 7

  9. The Class Statement class <name>: <suite> 7

  10. The Class Statement class <name>: <suite> A class statement creates a new class and binds that class to <name> in the first frame of the current environment. 7

  11. The Class Statement class <name>: <suite> 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

  12. The Class Statement class <name>: <suite> The suite is executed when the class statement is executed. 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

  13. The Class Statement class <name>: <suite> The suite is executed when the class statement is executed. 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) >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... 7

  14. The Class Statement class <name>: <suite> The suite is executed when the class statement is executed. 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) >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' 7

  15. The Class Statement class <name>: <suite> The suite is executed when the class statement is executed. 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) >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' >>> Clown.dance() 'No thanks' 7

  16. The Class Statement class <name>: <suite> The suite is executed when the class statement is executed. 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) >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' >>> Clown.dance() 'No thanks' >>> Clown <class '__main__.Clown'> 7

  17. Object Construction 8

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

  19. 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') When a class is called: 8

  20. 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') When a class is called: 1.A new instance of that class is created: 8

  21. 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') When a class is called: An account instance 1.A new instance of that class is created: 8

  22. 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') When a class is called: An account instance 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. 8

  23. 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') When a class is called: An account instance 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. class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder 8

  24. 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') When a class is called: An account instance 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. class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder 8

  25. 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') When a class is called: An account instance 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. class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder 8

  26. 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') When a class is called: An account instance balance: 0 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. class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder 8

  27. 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') When a class is called: An account instance balance: 0 holder: 'Jim' 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. class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder 8

  28. 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') When a class is called: An account instance balance: 0 holder: 'Jim' 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. class Account: def __init__(self, account_holder): __init__ is called self.balance = 0 a constructor self.holder = account_holder 8

  29. 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' When a class is called: An account instance balance: 0 holder: 'Jim' 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. class Account: def __init__(self, account_holder): __init__ is called self.balance = 0 a constructor self.holder = account_holder 8

  30. 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 0 When a class is called: An account instance balance: 0 holder: 'Jim' 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. class Account: def __init__(self, account_holder): __init__ is called self.balance = 0 a constructor self.holder = account_holder 8

  31. Object Identity 9

  32. Object Identity Every object that is an instance of a user-defined class has a unique identity: 9

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

  34. Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('Jim') >>> b = Account('Jack') Every call to Account creates a new Account instance. There is only one Account class. 9

  35. Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('Jim') >>> b = Account('Jack') Every call to Account creates a new Account >>> a.balance instance. There is only one Account class. 0 >>> b.holder 'Jack' 9

  36. Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('Jim') >>> b = Account('Jack') Every call to Account creates a new Account >>> a.balance instance. There is only one Account class. 0 >>> b.holder 'Jack' Identity operators "is" and "is not" test if two expressions evaluate to the same object: 9

  37. Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('Jim') >>> b = Account('Jack') Every call to Account creates a new Account >>> a.balance instance. There is only one Account class. 0 >>> b.holder 'Jack' Identity operators "is" and "is not" test if two expressions evaluate to the same object: >>> a is a True >>> a is not b True 9

  38. Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('Jim') >>> b = Account('Jack') Every call to Account creates a new Account >>> a.balance instance. There is only one Account class. 0 >>> b.holder 'Jack' Identity operators "is" and "is not" test if two expressions evaluate to the same object: >>> a is a True >>> a is not b True Binding an object to a new name using assignment does not create a new object: 9

  39. Object Identity Every object that is an instance of a user-defined class has a unique identity: >>> a = Account('Jim') >>> b = Account('Jack') Every call to Account creates a new Account >>> a.balance instance. There is only one Account class. 0 >>> b.holder 'Jack' Identity operators "is" and "is not" test if two expressions evaluate to the same object: >>> a is a True >>> a is not b True Binding an object to a new name using assignment does not create a new object: >>> c = a >>> c is a True 9

  40. Methods

  41. Methods 11

  42. Methods Methods are functions defined in the suite of a class statement 11

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

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

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

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

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

  48. 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 self should always be bound to an instance of the Account class def deposit(self, amount): 11

  49. 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 self should always be bound to an instance of the Account class def deposit(self, amount): self.balance = self.balance + amount 11

  50. 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 self should always be bound to an instance of the Account class def deposit(self, amount): self.balance = self.balance + amount return self.balance 11

  51. 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 self should always be bound to an instance of the Account class def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): 11

  52. 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 self should always be bound to an instance of the Account class def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: 11

  53. 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 self should always be bound to an instance of the Account class def deposit(self, amount): self.balance = self.balance + amount return self.balance def withdraw(self, amount): if amount > self.balance: return 'Insufficient funds' 11

  54. 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 self should always be bound to an instance of the Account class 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

  55. 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 self should always be bound to an instance of the Account class 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

  56. 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 self should always be bound to an instance of the Account class 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

  57. 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 self should always be bound to an instance of the Account class 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 s 11

  58. Invoking Methods 12

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

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

  61. 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. Defined with two parameters class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance 12

  62. 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. Defined with two parameters class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance Dot notation automatically supplies the first argument to a method. 12

  63. 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. Defined with two parameters class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance Dot notation automatically supplies the first argument to a method. >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 12

  64. 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. Defined with two parameters class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance Dot notation automatically supplies the first argument to a method. >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Invoked with one argument 12

  65. 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. Defined with two parameters class Account: ... def deposit(self, amount): self.balance = self.balance + amount return self.balance Dot notation automatically supplies the first argument to a method. >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Bound to self Invoked with one argument 12

  66. Dot Expressions 13

  67. Dot Expressions Objects receive messages via dot notation. 13

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