New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

CS 61A Week 7 Lecture

by: Scott Lee

CS 61A Week 7 Lecture CS 61A

Scott Lee

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Classes and objects
The Structure and Interpretation of Computer Programs
John DeNero
Class Notes
25 ?




Popular in The Structure and Interpretation of Computer Programs

Popular in Elect Engr & Computer Science

This 5 page Class Notes was uploaded by Scott Lee on Sunday October 2, 2016. The Class Notes belongs to CS 61A at University of California Berkeley taught by John DeNero in Fall 2016. Since its upload, it has received 3 views. For similar materials see The Structure and Interpretation of Computer Programs in Elect Engr & Computer Science at University of California Berkeley.

Popular in Elect Engr & Computer Science


Reviews for CS 61A Week 7 Lecture


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/02/16
61A Week 7 Announcements Object-Oriented Programming Object-Oriented Programming A method for organizing programs • Data abstraction • Bundling together information and related behavior A 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 method calls • Method calls are messages passed between objects • Several objects may all be instances of a common type • Different types may relate to each other Specialized syntax & vocabulary to support this metaphor 4 John's Account Jack's Account John Withdraw $10 Deposit $10 Apply for a loan! Classes A class serves as a template for its instances Idea: 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('John') >>> a.holder 'John' >>> a.balance 0 >>> a.deposit(15) 15 >>> a.withdraw(10) 5 >>> a.balance 5 >>> a.withdraw(10) 'Insufficient funds' Better idea: All bank accounts share a withdraw method and a deposit method 5 Class Statements The Class Statement 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 The suite is executed when the class statement is executed. >>> class Clown: ... nose = 'big and red' ... def dance(): ... return 'No thanks' ... >>> Clown.nose 'big and red' >>> 'No thanks' >>> Clown <class '__main__.Clown'> class <name>: <suite> When a class is called: 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 An account instance 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 class Account: def __init__(self, account_holder): self.balance = 0 self.holder = account_holder 8 balance: 0 holder: 'Jim' __init__ is called a constructor Object Identity >>> a = Account('John') >>> b = Account('Jack') >>> a is a True >>> a is not b True Every 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 operators "is" and "is not" test if two expressions evaluate to the same object: >>> c = a >>> c is a True 9 Every call to Account creates a new Account instance. There is only one Account class. >>> a.balance 0 >>> b.holder 'Jack' Methods 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): 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 self should always be bound to an instance of the Account class s 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 >>> tom_account = Account('Tom') >>> tom_account.deposit(100) 100 Dot notation automatically supplies the first argument to a method Invoked with one argument Defined with two parameters 12 Bound to self Dot Expressions Objects receive messages via dot notation Dot notation accesses attributes of the instance or its class <expression> . <name> The <expression> can be any valid Python expression The <name> must be a simple name Evaluates to the value of the attribute looked up by <name> in the object that is the value of the <expression> tom_account.deposit(10) Dot expression Call expression 13 (Demo) Attributes (Demo) Accessing Attributes Using getattr, we can look up an attribute using a string >>> getattr(tom_account, 'balance') 10 >>> hasattr(tom_account, 'deposit') True getattr and dot expressions look up a name in the same way Looking up an attribute name in an object may return: • One of its instance attributes, or • One of the attributes of its class 15 Methods and Functions Python distinguishes between: • Functions, which we have been creating since the beginning of the course, and • Bound methods, which couple together a function and the object on which that method will be invoked Object + Function = Bound Method >>> type(Account.deposit) <class 'function'> >>> type(tom_account.deposit) <class 'method'> >>> Account.deposit(tom_account, 1001) 1011 >>> tom_account.deposit(1004) 2015 16 Function: all arguments within parentheses Method: One object before the dot and other arguments within parentheses 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 17 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 The interest attribute is not part of the instance; it's part of the class! 18 >>> tom_account = Account('Tom') >>> jim_account = Account('Jim') >>> tom_account.interest 0.02 >>> jim_account.interest 0.02


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Kyle Maynard Purdue

"When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the I made $280 on my first study guide!"

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.