CS61A Lecture 15&Mutable Values16 Mutable Functions

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值