copy和deepcopy

https://blog.csdn.net/u014745194/article/details/70271868



```python
import copy
a=[11,22,33]
b=[11,22,33]
```

### 比较两个内容相同的值,==比较的是内容,“is”比较的是指向对象(地址)


```python
print(id(a),id(b))
print(a==b)  
print(a is b)
```

    2387717525704 2387717974088
    True
    False
    

### 当深拷贝时(只是内容),“==”比较的是内容,“is”比较是对象


```python
c=copy.deepcopy(a)
print(id(a),id(c))
print(a==c)  
print(a is c)
```

    2387717660872 2387717656072
    True
    False
    


```python
### d=a(地址和内容赋值),“==”比较的是内容,“is”比较的是对象
```


```python
d=a
print(id(a),id(d))
print(a==d)  
print(a is d)
```

    2387717660872 2387717660872
    True
    True
    

# 深拷贝 和浅拷贝


```python
#①浅拷贝是对于一个对象的顶层拷贝,通俗的理解是:拷贝了引用,并没有拷贝内容
a=[11,22,33]
b=a
print(id(a),id(b))

```

    2387717956040 2387717956040
    


```python
#②深拷贝是对于一个对象所有层次的拷贝(递归),就是自己开辟一个空间,内容一样
a=[11,22,33]
b=copy.deepcopy(a)
print(id(a),id(b))#a=[11,22,33]
a.append(44)
print(a,b)#[11, 22, 33, 44] [11, 22, 33]
```

    2387717957832 2387717955336
    [11, 22, 33, 44] [11, 22, 33]
    


```python
##③deepcopy和copy的区别
a=[11,22,33]
b=[5,6,7]
#第一层copy和第一层deepcopy
c=copy.copy(a)
print(id(a),id(c))
a.append(44)
print(a,c)
'''
2387717655304 2387735256968
[11, 22, 33, 44] [11, 22, 33]


'''
# d=copy.deepcopy(a)
# print(id(a),id(d))
# a.append(44)
# print(a,d)
'''
2387735019272 2387717139912
[11, 22, 33, 44] [11, 22, 33]
'''
```

    2387717655304 2387735256968
    [11, 22, 33, 44] [11, 22, 33]
    




    '\n2387735019272 2387717139912\n[11, 22, 33, 44] [11, 22, 33]\n'




```python
a=[11,22,33]
b=[5,6,7]
c=[a,b]

#第二层copy和deepcopy的区别
# d=copy.copy(c)
# print(id(c),id(d))
# a.append(44)
# print(c,d)
'''
2387735208712 2387735230728
[[11, 22, 33, 44], [5, 6, 7]] [[11, 22, 33, 44], [5, 6, 7]]

'''

e=copy.deepcopy(c)
print(id(c),id(e))
a.append(44)
print(c,e)
'''
2387717420744 2387735208712
[[11, 22, 33, 44], [5, 6, 7]] [[11, 22, 33], [5, 6, 7]]

'''
```

    2387717420744 2387735208712
    [[11, 22, 33, 44], [5, 6, 7]] [[11, 22, 33], [5, 6, 7]]
    


```python
## ④使用copy模块的copy功能时,它会根据当前数据类型是可变和不可变类型处理不同,
#可变类型:
#       第一层深拷贝,第二层浅拷贝。
#不可变类型:
#       第一层第二层都是浅拷贝
a=[1,2,3]
b=[4,5,6]
c=(a,b)

e=copy.copy(c)
print(id(c),id(e))  #2387717464200 2387717464200

d=copy.deepcopy(c)
print(id(c),id(d))  #2387717632648 2387717512200
```

    2387717632648 2387717632648
    2387717632648 2387717512200
    

# 生成器
generator=(x for x in range(5))
next(generator)
next(generator)
next(generator)
next(generator)
next(generator)
next(generator)


```python
generator=(x for x in range(5))
for g in generator:
    print(g)
```

    0
    1
    2
    3
    4
    


```python
def fib(times):
    n=0
    a,b=0,1
    for i in range(times):
        print("---------------1")
        yield b
        print("---------------2")
        a,b=b,a+b
        n+=1
          
        
        
fib=fib(5)
next(fib)
next(fib)
```

    ---------------1
    ---------------2
    ---------------1
    




    1



## __next__和send的区别


```python
def test():
    i=0
    while i<5:
        temp=yield i
        print(temp)
        i+=1
t=test()
t.__next__()  #输出为0 停在了yield i ,yield出0
t.__next__()   #继续执行,将yield i赋值给temp而yield i没有值,所以print(temp)为None,又一次yield出1
```

    None
    




    1




```python
t.send("haha")#,将“haha"赋值给了temp,所以就打印出temp值
```

    haha
    




    2




```python
###注意
```


```python
def test():
    i=0
    while i<5:
        temp=yield i
        print(temp)
        i+=1
t=test()
t.send("haha")
```


    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    <ipython-input-83-83eb7db9c449> in <module>()
          6         i+=1
          7 t=test()
    ----> 8 t.send("haha")
    

    TypeError: can't send non-None value to a just-started generator



```python
#①解决办法
t.__next__()
t.send("hhaha")
```

    hhaha
    




    1




```python
#②解决方法
t.send("")
```

    
    


    ---------------------------------------------------------------------------

    StopIteration                             Traceback (most recent call last)

    <ipython-input-82-f56aa61f06c2> in <module>()
          1 #②解决方法
    ----> 2 t.send("")
    

    StopIteration: 



```python
#temp=yield i选择性执行
```


```python
def test():
    i=0
    while i<5:
        if i==0:
            temp=yield i
            print(temp)
        else:
            yield i
        i+=1
t=test()
t.__next__()
t.send("hhaa")
```

    hhaa
    




    1




```python
t.send("hhaa")
```




    2




```python
t.send("hhaa")
```




    3



### yield的作用,一方面是多任务(协程的一种)


```python
def test1():
    while True:
        print("--1--")
        yield None
def test2():
    while True:
        print("--2--")
        yield None
        
t1=test1()
t2=test2()
while True:
    t1.__next__()
    t2.__next__()

```

# 迭代器

 迭代是访问集合元素的一种方式。迭代器是一个可以记住遍历的位置的对象。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。
 
 1. 可迭代对象

    以直接作用于 for 循环的数据类型有以下几种:

    一类是集合数据类型,如 list 、 tuple 、 dict 、 set 、 str 等;

    一类是 generator ,包括生成器和带 yield 的generator function。

    这些可以直接作用于 for 循环的对象统称为可迭代对象: Iterable 。
    
 生成器一定是迭代器,但是迭代器不一定是生成器,但是迭代器包含生成器

# 闭包
### 在函数内部再定义一个函数,并且这个函数用到了外边函数的变量,那么将这个函数以及用到的一些变量称之为闭包


```python
def line_conf(a, b):
    def line(x):
        return a*x + b
    return line

line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5))
print(line2(5))
```

    6
    25
    

# 装饰器


```python
def add(x,y):
    z=x+y
    print(z)
    
a=add(3,4)
print(a)
```

    7
    None
    


```python
def log(func):
    def wrapper(*args, **kw):
        print('call %s():' % func.__name__)
        return func(*args, **kw)
    return wrapper
@log
def now():
    print('2015-3-25')
```


```python
now()
```

    call now():
    2015-3-25
    

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值