DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 14

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

61A Lecture 14Friday, September 30Friday, September 30, 2011The Story So Far About DataData abstraction: Enforce a separation between how data values are represented and how they are used.Abstract data types: A representation of a data type is valid if it satisfies certain behavior conditions. Message passing: We can organize large programs by building components that relate to each other by passing messages.Dispatch functions/dictionaries: A single object can include many different (but related) behaviors that all manipulatethe same local state.2(All of these techniques can be implemented using only functions and assignment.)Friday, September 30, 2011Object-Oriented ProgrammingA method for organizing modular programs•Abstraction barriers•Message passing•Bundling together information and related behaviorA 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 the messages it receives.•Several objects may all be instances of a common type.•Different types may relate to each other as well.Specialized syntax & vocabulary to support this metaphor3Friday, September 30, 2011ClassesA class serves as a template for its instances.4Idea: 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.balance0>>> a.deposit(15)15>>> a.withdraw(10)5>>> a.balance5>>> a.withdraw(10)'Insufficient funds'Better idea: All bank accounts share a "withdraw" method.Friday, September 30, 2011The Class StatementA class statement creates a new class and binds that class to <name> in the first frame of the current environment.Statements in the <suite> create attributes of the class.As soon as an instance is created, it is passed to __init__, which is an attribute of the class.5class <name>(<base class>): <suite> class Account(object): def __init__(self, account_holder): self.balance = 0 self.holder = account_holderNext lectureFriday, September 30, 2011Classes are "called" to construct instances.The constructor __init__ is called on newly created instances.The object is bound to __init__'s first parameter, self.Initialization6Idea: All bank accounts have a balance and an account holder; the Account class should add those attributes.>>> a = Account('Jim')>>> a.holder'Jim'>>> a.balance0 class Account(object): def __init__(self, account_holder): self.balance = 0 self.holder = account_holderFriday, September 30, 2011Object Identity7>>> a = Account('Jim')>>> b = Account('Jack')>>> a is aTrue>>> a is not bTrueEvery 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 testing is performed by "is" and "is not" operators:>>> c = a>>> c is aTrueFriday, September 30, 2011MethodsMethods are defined in the suite of a class statement8 class Account(object): 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.balanceThese def statements create function objects as always,but their names are bound as attributes of the class.Friday, September 30, 2011Invoking MethodsAll invoked methods have access to the object via the self parameter, and so they can all access and manipulate the object's state.9 class Account(object): ... def deposit(self, amount): self.balance = self.balance + amount return self.balance>>> tom_account = Account('Tom')>>> tom_account.deposit(100)100Dot notation automatically supplies the first argument to a method.Invoked with one argumentCalled with two argumentsFriday, September 30, 2011Dot ExpressionsObjects receive messages via dot notationDot notation accesses attributes of the instance or its class10<expression> . <name>The <expression> can be any valid Python expressionThe <name> must be a simple nameEvaluates to the value of the attribute looked up by <name>on the object that is the value of the <expression>tom_account.deposit(10)Dot expressionCall expressionFriday, September 30, 2011Accessing AttributesUsing getattr, we can look up an attribute using a string, just as we did with a dispatch function/dictionary11>>> getattr(tom_account, 'balance')10>>> hasattr(tom_account, 'deposit')Truegetattr and dot expressions look up a name in the same wayLooking up a named attribute on an object may return:•One of its instance attributes•One of the attributes (including a method) of its classFriday, September 30, 2011Methods and FunctionsPython distinguishes between:•function objects, which we have been creating since the beginning of the course, and •bound method objects, which couple together a function and the object on which that method will be invoked12Object + Function Object = Bound Method Object>>> type(Account.deposit)<class 'function'>>>> type(tom_account.deposit)<class 'method'>>>> Account.deposit(tom_account, 1001)1011>>> tom_account.deposit(1000)2011Friday, September 30, 2011Looking Up Attributes by Name13<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 <name> does not appear among instance attributes, it is looked up in the class, which yields a class attribute value. 4. That value is returned unless it is a function value, in which case a bound method value is returned instead.Friday, September 30, 2011Class AttributesClass attributes are "shared" across all instances of a class because they are attributes of the class, not the instance.14 class Account(object): 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.interest0.02>>> jim_account.interest0.02interest is not part of the instance that was somehow copied from the class!Friday, September 30, 2011Assignment Statements and


View Full Document

Berkeley COMPSCI 61A - Lecture 14

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 14
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 14 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 14 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?