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