Lecture 15 Mutable Values
1.objects
objects 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
In python , every value is an object
All objects have attributes
A lot of data manipulation happens through object methods
2.Mutable Operations
only objects of mutable types can change:lists&dictionaries
mutable poeratins e.g.
.append .extend .pop
3.Tuple
tuples values are protected from mutation
tuples are immutable sequences
4.mutation
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
>>>a = [10]
>>>b = a
>>>a == b
True
>>>a.append(20)
>>>a ==b
True
>>>a
[10, 20]
>>>b
[10, 20]
two lists have the same values also can be different
>>>a = [10]
>>>b = [10]
>>>a == b
True
>>>a.append(20)
>>>a ==b
False
>>>a
[10, 20]
>>>b
[10]
Identity
<exp0> is <exp1>
they evaluate to the same object
Equality
<exp0> ==<exp1>
they evaulate to the equal values
Identicla objects are always equal values
5.Mutable default arguments are dangerous
A default argument value is part of a function value, not generated by the call.
>>>def f(s=[]):
s.append(5)
return len(s)
>>>f()
1
>>>>f()
2
>>>f()
3
Lecture 16 Mutable Functions
1.nonlocal statement
2.use changeble stucture(e.g.list)
def make_withdraw_list(balancd):
b = [balance]
def withdraw(zmount):
if amount > n[0]:
reeturn 'Insufficient funds'
b[0] = b[0] - amount
return b[0]
return withdraw