CS61A Lecture 14: Mutable Data

2 Pages
94 Views
Unlock Document

Department
Computer Science
Course
COMPSCI 61A
Professor
De Nero
Semester
Fall

Description
Mutable Functions: A function with behavior that varies over time Ex: Bank account withdrawal, non-pure function Each time output is different where is balance stored? Higher order functions Non-local assignment def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): nonlocal balance if amount > balance: return 'Insufficient funds' balance = balance - amount return balance return withdraw Declare name nonlocal at top of the function in which it is re-assigned Bind name to nonlocal name Nonlocal Assignment nonlocal Effects: Future assignments to name change its pre-existing binding in first non-local frame of current environment in which name is bound Names listed in nonlocal statement must refer to pre-existing bindings in enclosing scope Names listed in nonlocal statement must not collide with pre-existing bindings in local scope Python Particulars Python precomputes which frame contains each name before executing body of a function All instances must refer to same frame in body of function Mutable Values &
More Less

Related notes for COMPSCI 61A

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit