Objects (Demo1)
Objects • Objects represent information • They consist of data and behavior, bundled together to create abstractions • Objects can represent things, but also properties, interactions, & processes • A type of object is called a class; classes are first-class values in Python • Object-oriented programming: • A metaphor for organizing large programs • Special syntax that can improve the composition of programs • In Python, every value is an object • All objects have attributes • A lot of data manipulation happens through object methods • Functions do one thing; objects do many related things 2
Example: Strings
Representing Strings: the ASCII Standard American Standard Code for Information Interchange “Bell” (\a) “Line Feed” (\n) 0 0 0 8 rows: 3 bits 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 16 columns: 4 bits • Layout was chosen to support sorting by character code • Rows indexed 2-5 are a useful 6-bit (64 element) subset • Control characters were designed for transmission (Demo2) 4
https://en.wikipedia.org/wiki/List_of_Unicode_characters Representing Strings: the Unicode Standard • 137K characters • 146 scripts (organized) • Enumeration of character properties, such as case • Supports bidirectional display order • A canonical name for every character http://ian-albert.com/unicode_chart/unichart-chinese.jpg LATIN CAPITAL LETTER A ' 🏁 ' ' ♪ ' BASKETBALL AND HOOP (Demo3) EIGHTH NOTE 5
Mutation Operations
https://en.wikipedia.org/wiki/Playing_card_suit Some Objects Can Change [Demo4] First example in the course of an object changing state The same object can change in value throughout the course of computation 👪 👶 👨 👷 jessica same_person Unicode character OLDER WOMAN GIRL BABY name WOMAN All names that refer to the same object are affected by a mutation Only objects of mutable types can change: lists & dictionaries {Demo5} 7
Mutation Can Happen Within a Function Call A function can change the value of any object in its scope def mystery(s): def mystery(s): or >>> four = [1, 2, 3, 4] s.pop() s[2:] = [] >>> len(four) s.pop() 4 >>> mystery(four) >>> len(four) 2 def another_mystery(): >>> four = [1, 2, 3, 4] four.pop() >>> len(four) four.pop() 4 >>> another_mystery() # No arguments! >>> len(four) 2 8 pythontutor.com/composingprograms.html#code=def%20mystery%28s%29%3A%0A%20%20%20%20s.pop%28%29%0A%20%20%20%20s.pop%28%29%0A%0Afour%20%3D%20[1,%202,%203,%204]%0Amystery%28four%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0
Tuples (Demo6)
Tuples are Immutable Sequences Immutable values are protected from mutation >>> turtle = (1, 2, 3) >>> turtle = [1, 2, 3] >>> ooze() >>> ooze() Next lecture: ooze can >>> turtle >>> turtle change turtle's binding (1, 2, 3) ['Anything could be inside!'] The value of an expression can change because of changes in names or objects >>> x = 2 >>> x = [1, 2] >>> x + x >>> x + x 4 [1, 2, 1, 2] Name change: Object mutation: >>> x = 3 >>> x.append(3) >>> x + x >>> x + x 6 [1, 2, 3, 1, 2, 3] An immutable sequence may still change if it contains a mutable value as an element >>> s = ([1, 2], 3) >>> s = ([1, 2], 3) >>> s[0][0] = 4 >>> s[0] = 4 >>> s ERROR ([4, 2], 3) 10
Mutation
Sameness and Change • As long as we never modify objects, a compound object is just the totality of its pieces • A rational number is just its numerator and denominator • This view is no longer valid in the presence of change • A compound data object has an "identity" in addition to the pieces of which it is composed • A list is still "the same" list even if we change its contents • Conversely, we could have two lists that happen to have the same contents, but are different >>> a = [10] >>> a = [10] >>> b = a >>> b = [10] >>> a == b >>> a == b True True >>> a.append(20) >>> b.append(20) >>> a >>> a [10, 20] [10] >>> b >>> b [10, 20] [10, 20] >>> a == b >>> a == b True False 12
Identity Operators Identity <exp0> is <exp1> evaluates to True if both <exp0> and <exp1> evaluate to the same object Equality <exp0> == <exp1> evaluates to True if both <exp0> and <exp1> evaluate to equal values Identical objects are always equal values (Demo) 13
Mutable Default Arguments are Dangerous A default argument value is part of a function value, not generated by a call >>> def f(s=[]): ... s.append(3) ... return len(s) ... >>> f() 1 >>> f() Each time the function 2 is called, s is bound >>> f() to the same value! 3 14 pythontutor.com/composingprograms.html#code=def%20f%28s%3D[]%29%3A%0A%20%20%20%20s.append%283%29%0A%20%20%20%20return%20len%28s%29%0A%20%20%20%20%0Af%28%29%0Af%28%29%0Af%28%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=0
Mutable Functions
Functions with behavior that changes over time def square(x): def f(x): return x * x ... Returns the same value when >>> square(5) >>> f(5) called with the same input 25 25 >>> square(5) >>> f(5) 25 26 Return value is different when called with the same >>> square(5) >>> f(5) input 25 27 16
Example - Withdraw Let's model a bank account that has a balance of $100 Argument: amount to withdraw >>> withdraw(25) Return value: remaining balance 75 >>> withdraw(25) Second withdrawal of the same amount 50 Different return value! >>> withdraw(60) Where's this balance stored? 'Insufficient funds' >>> withdraw(15) >>> withdraw = make_withdraw(100) 35 Within the parent frame of A function has a body and the function! a parent environment 17
Persistent Local State Using Environments The parent frame contains the balance, the local state of the withdraw function All calls to the Every call decreases the same balance by same function have (a possibly different) amount the same parent 18 pythontutor.com/composingprograms.html#code=def%20make_withdraw%28balance%29%3A%0A%20%20%20%20def%20withdraw%28amount%29%3A%0A%20%20%20%20%20%20%20%20nonlocal%20balance%0A%20%20%20%20%20%20%20%20if%20amount%20>%20balance%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20'Insufficient%20funds'%0A%20%20%20%20%20%20%20%20balance%20%3D%20balance%20-%20amount%0A%20%20%20%20%20%20%20%20r
Reminder: Local Assignment Assignment binds name(s) to value(s) in the first frame of the current environment Execution rule for assignment statements: 1. Evaluate all expressions right of =, from left to right 2. Bind the names on the left to the resulting values in the current frame 19 pythontutor.com/composingprograms.html#code=def%20percent_difference%28x,%20y%29%3A%0A%20%20%20%20%20%20%20%20difference%20%3D%20abs%28x-y%29%0A%20%20%20%20%20%20%20%20return%20100%20*%20difference%20/%20x%0Adiff%20%3D%20percent_difference%2840,%2050%29&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=4
Non-Local Assignment & Persistent Local State def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal at the top of the nonlocal balance body of the function in which it is re-assigned if amount > balance: return 'Insufficient funds' balance = balance - amount Re-bind balance in the first non-local frame in which it was bound previously return balance return withdraw (Demo) 20
Non-Local Assignment
The Effect of Nonlocal Statements nonlocal <name>, <name>, ... Effect : Future assignments to that name change its pre-existing binding in the first non-local frame of the current environment in which that name is bound. Python Docs: an "enclosing scope" From the Python 3 language reference : Names listed in a nonlocal statement must refer to pre-existing bindings in an enclosing scope. Names listed in a nonlocal statement must not collide with pre-existing bindings in the local scope. Current frame http://docs.python.org/release/3.1.3/reference/simple_stmts.html#the-nonlocal-statement http://www.python.org/dev/peps/pep-3104/ 22
Recommend
More recommend