Python 强化训练:第六篇

作者: 谢小路 | 来源:发表于2016-11-05 11:48 被阅读128次

强化训练:第六篇


1. 深浅拷贝:是否是同一个对象,使用id判断是否指向同一个对象, 深浅拷贝,引用区分可变对象和不可变对象


# 赋值法创建引用, 指向同一对象, id值相同

foo1 = 3
foo2 = foo1
print("id = ", id(foo1), "id = ", id(foo2))
#id =  1493167984 id =  1493167984

foo1 = 4
print(foo2, foo1)
#3 4
# 整型不可变对象, python 会缓存数据

foo3 = 3
foo4 = 2 + 1
print("id = ", id(foo3), "id = ", id(foo4))
#id =  1493167984 id =  1493167984


# python 仅缓存简单整型

foo5 = 3.14
foo6 = 2.14 + 1
foo7 = foo5
print("id = ", id(foo5), "id = ", id(foo6), "id = ", id(foo7))
#id =  3449480 id =  3449504 id =  3449480

# 可变对象, 引用, 浅拷贝影响源数据, 深拷贝指向不同对象

## 引用

list_one = [1, 2, 3]
list_two = list_one
print("id = ", id(list_one), "id = ", id(list_two))
# id =  12186120 id =  12186120
list_one[2] = 4
print(list_two)
#[1, 2, 4]

list_two[1] = 5
print(list_one)
print("id = ", id(list_one), "id = ", id(list_two))

#[1, 5, 4]
#id =  12186120 id =  12186120

## 深浅拷贝

from copy import copy, deepcopy

list_three = [1, 2, [3, 4]]
list_four = list_three    # 引用:指向相同对象
list_five = copy(list_three)    # 浅拷贝
list_six = deepcopy(list_three)    # 深拷贝
print(id(list_three), id(list_four), id(list_five), id(list_six))
#12185736 12185736 12321864 12127880

list_four[1] = 1156143589
print("list_three= ", list_three, "list_five= ", list_five, "list_six= ", list_six)
# list_three=  [1, 1156143589, [3, 4]] list_five=  [1, 2, [3, 4]] list_six=  [1, 2, [3, 4]]

list_three[0] = 88688
print("list_four= ", list_four, "list_five= ", list_five, "list_six= ", list_six)
#list_four=  [88688, 1156143589, [3, 4]] list_five=  [1, 2, [3, 4]] list_six=  [1, 2, [3, 4]]


list_five[0] = 4444
print("list_three= ", list_three, "list_four= ", list_four)
# list_three=  [88688, 1156143589, [3, 4]] list_four=  [88688, 1156143589, [3, 4]]

list_five[2][1] = 666666
print("list_three= ", list_three, "list_four= ", list_four, "list_six= ", list_six)
#list_three=  [88688, 1156143589, [3, 666666]] list_four=  [88688, 1156143589, [3, 666666]] list_six=  [1, 2, [3, 4]]

list_six[2][0] = 99999
print("list_three= ", list_three, "list_four= ", list_four, "list_five= ", list_five, "list_six= ", list_six)
#list_three=  [88688, 1156143589, [3, 666666]] list_four=  [88688, 1156143589, [3, 666666]] list_five=  [4444, 2, [3, 666666]] list_six=  [1, 2, [99999, 4]]

2. 总结

  1. 引用指向同一对象:相同id, 不可变对象独立操作,可变对象相互影响
  2. 浅拷贝:只拷贝父对象, 嵌套可变对象会影响数据源
  3. 深拷贝还会拷贝对象的内部的子对象:独立操作,不影响数据源

相关文章

  • Python 强化训练:第六篇

    强化训练:第六篇 1. 深浅拷贝:是否是同一个对象,使用id判断是否指向同一个对象, 深浅拷贝,引用区分可变对象和...

  • Jupyter Notebook--学习python必不可少的工

    这是树哥讲python的第六篇文章。我们之前一直讲的python应用案例都在IDLE中执行,这就导致了编程窗口和执...

  • Python正则表达式-为什么要用原始字符串

    本文为《爬着学Python》系列第六篇文章。本文也算是系列教程的第二篇不规则更新。 在讨论Python正则表达式之...

  • Python 强化训练:第一篇

    强化训练: 第一篇 目标 0. 校招算是结束了吧!简单回顾几句: 校招python岗位极少,多是初创型公司对pyt...

  • Python 强化训练:第十一篇

    主题 正常情况下,程序的运行按顺序执行,但是涉及某些操作,等待结果完成却是非常耗时的操作,比如爬虫进行IO操作等,...

  • Python 强化训练:第五篇

    强化训练:第五篇 主题:函数 基本定义方法 任意数量参数 只接受关键字参数 显示数据类型 默认参数 匿名函数 N个...

  • Python 强化训练:第四篇

    强化训练:第四篇 问题来源 面向对象的语言的重要特性是存在类的概念 内容 新式类和旧式类 定义类的属性和“访问权限...

  • Python 强化训练:第三篇

    强化训练:第三篇 问题来源 pythoner面试经常会问到迭代器和生成器的区别 内容 可迭代对象 迭代器:正向迭代...

  • Python 强化训练:第二篇

    强化训练:第二篇 摘要:心好累. 问题来源 爬虫中会经常会遇到字符串的处理 主要内容 拆分字符串 字符串开头结尾 ...

  • Python 强化训练:第九篇

    主题 数据处理 csv文件 json文件 xml: xpath excel 1. CSV: 逗号分隔值,其文件以纯...

网友评论

    本文标题:Python 强化训练:第六篇

    本文链接:https://www.haomeiwen.com/subject/zbtguttx.html