1、a=a+2,表示一个新的对象,新的对象名字还是a,但是指向的内存地址已经变了

>>> a=2
>>> id(a)
140406287260016
>>> a=a+2
>>> a
4
>>> id(a)
140406287259968

所以对于tuple对象(不可变对象),也是可以这样操作的

>>> tuple1=(1,2)
>>> id(tuple1)
4521580448
>>> tuple1=tuple1+(3,)
>>> tuple1
(1, 2, 3)
>>> id(tuple1)
4521658880

2、a+=2对于有些对象的操作是表示原来的对象,对有些对象的操作是生成了一个新对象

不可变对象tuple1,操作完后,内存地址已经发生变化,生成一个新的对象
>>> tuple1=(1,2)
>>> type(tuple1)
<type 'tuple'>
>>> tuple1+=(3,)
>>> id(tuple1)
4521658880
>>> tuple1+=(4,5)
>>> id(tuple1)
4520649072

而list对象,可变对象,+=操作、append操作、extend操作,都是在原对象上操作

>>> list1=[1,2]
>>> id(list1)
4521614656
>>> list1+=[3]
>>> id(list1)
4521614656
>>> list1.append(4)
>>> id(list1)
4521614656
>>> list1.extend(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> list1.extend([5])
>>> id(list1)
4521614656
>>>

3、

x = [1,2,3]
print "before func(), global! x = ",x,"id(x) = ",id(x) def func():
global x
print "in func(), local! original x = ",x,"id(x) = ",id(x)
x = x + [1]
print "in func(), local! now x = ",x,"id(x) = ",id(x) func()
print "after func(), global! x = ",x,"id(x) = ",id(x)
结果:
[python] view plain copy
before func(), global! x = [1, 2, 3] id(x) = 47781768
in func(), local! original x = [1, 2, 3] id(x) = 47781768
in func(), local! now x = [1, 2, 3, 1] id(x) = 47795720
after func(), global! x = [1, 2, 3, 1] id(x) = 47795720 global就保证了,即使我的变量x在函数中指向对象变了,外部的x也会指向新的对象
x = [1,2,3]
print "before func(), global! x = ",x,"id(x) = ",id(x) def func(x):
print "in func(), local! original x = ",x,"id(x) = ",id(x)
x = x + [1]
print "in func(), local! now x = ",x,"id(x) = ",id(x) func(x)
print "after func(), global! x = ",x,"id(x) = ",id(x)
结果:
before func(), global! x = [1, 2, 3] id(x) = 46339976
in func(), local! original x = [1, 2, 3] id(x) = 46339976
in func(), local! now x = [1, 2, 3, 1] id(x) = 46390664
after func(), global! x = [1, 2, 3] id(x) = 46339976 x = x + [1],是新建了一个对象,id(x) =  46390664
利用id(x),查看下x += [1]对象是怎么变化的吧:
x = [1,2,3]
print "before func(), global! x = ",x,"id(x) = ",id(x) def func(x):
print "in func(), local! original x = ",x,"id(x) = ",id(x)
x += [1]
print "in func(), local! now x = ",x,"id(x) = ",id(x) func(x)
print "after func(), global! x = ",x,"id(x) = ",id(x)
结果:
before func(), global! x = [1, 2, 3] id(x) = 46536584
in func(), local! original x = [1, 2, 3] id(x) = 46536584
in func(), local! now x = [1, 2, 3, 1] id(x) = 46536584
after func(), global! x = [1, 2, 3, 1] id(x) = 46536584
id(x)全程一样,x += [1],python直接就在原对象上操作

参考:

1、http://blog.csdn.net/emaste_r/article/details/47395055

python中a=a+2与a+=2的区别的更多相关文章

  1. python中urllib, urllib2,urllib3, httplib,httplib2, request的区别

    permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...

  2. python中生成器对象和return 还有循环的区别

    python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...

  3. Python中%r和%s的详解及区别_python_脚本之家

    Python中%r和%s的详解及区别_python_脚本之家 https://www.jb51.net/article/108589.htm

  4. python中os.path.abspath与os.path.realpath 区别

    python中os.path.abspath与os.path.realpath 区别cd /homemkdir amkdir btouch a/1.txtln -s /home/a/1.txt /ho ...

  5. python 中的input()和raw_input()功能与使用区别

    在python中raw_input()和input()都是提示并获取用户输入的函数,然后将用户的输入数据存入变量中.但二者在处理返回数据类型上有差别. input()函数是raw_intput()和e ...

  6. python中List append()、extend()和insert()的区别

    Python中向列表增加更多数据时,有append().extend()和insert()等方法 其中最常用的是list.append(obj) 向列表的尾部添加一个新的元素. 需要一次性添加多个元素 ...

  7. 【Python深入】Python中继承object和不继承object的区别

    python中定义class的时候,有object和没有object的不同?例如: class Solution(object): class Solution(): 这俩的区别在于—————— 在p ...

  8. python中None与0、Null、false区别

    None是Python中的一个关键字,None本身也是个一个数据类型,而这个数据类型就是None,它可0.空字符串以及false均不一样,这些都只是对象,而None也是一个类. 给个bool测试: v ...

  9. Python中模块(Module)和包(Package)的区别

    本文绝大部分内容转载至:廖雪峰官方网站 1. 模块(Module) 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函 ...

  10. 接口测试基础——第7篇 Python中_、__、__func__之间的区别

    今天的东西很少,主要是给自己做个笔记,顺便帮大家普及一下Python中的边角知识: 1.if __name__ == "__main__"是什么意思 答:一个.py文件,如果是自身 ...

随机推荐

  1. ogre3D学习基础6---场景管理器的使用

    场景管理器的使用 最常使用的坐标系统空间(同时也是Ogre程序所能提供的)即是世界空间(World).父节点空间(Parent)以及本地空间(Local). 1.世界空间 就是物体所存在的地方,当我们 ...

  2. requests与urllib 库

    requests库 发送请求: 可以处理所有请求类型:get.post.put.Delete.Head.Options r = requests.get(''https://httpbin.org/' ...

  3. Tomcat 禁用PUT方法, 404/500错误重定向, 网站图标

    (1) Tomcat禁用Put等不安全方法. <security-constraint> <web-resource-collection> <web-resource- ...

  4. [adb 学习篇] python将adb命令集合到一个工具上

    https://testerhome.com/topics/6938 qzhi的更全面,不过意思是一样的,另外补充一个开源的https://github.com/264768502/adb_wrapp ...

  5. 【转】javascript操作Select标记中options集合

    先来看看options集合的这几个方法:options.add(option)方法向集合里添加一项option对象:options.remove(index)方法移除options集合中的指定项:op ...

  6. Python3网络爬虫(三):urllib.error异常

    运行平台:Windows Python版本:Python3.x IDE:Sublime text3 转载请注明作者和出处:http://blog.csdn.net/c406495762/article ...

  7. oracle报错处理

    oracle安装过程报错 报错一:Error in invoking target 'install' of makefile '/u01/app/oracle/product/11.2.0/dbho ...

  8. 【Luogu】P3979遥远的国度(树链剖分)

    题目链接 不会换根从暑假开始就困扰我了……拖到现在…… 会了还是很激动的. 换根操作事实上不需要(也不能)改树剖本来的dfs序……只是在query上动动手脚…… 设全树的集合为G,以root为根,u在 ...

  9. 【Luogu】P2805植物大战僵尸(拓扑排序+最大流)

    题目链接 这题数据xie强qwq.拓扑用的那个图建反了得80. 一眼看出(个屁,题解上都说一眼看出,然而我还是太蒻了)这是个最大权闭合图.从被保护植物向保护植物连边,然后跑最大流,用正点权和减去. 哦 ...

  10. eclipse Java EE安装和web项目的创建

    一.根据http://www.itnose.net/detail/6139800.html基本安装成功二.根据http://www.cnblogs.com/freebsd-pann/archive/2 ...