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. iphone使用keychain来存取用户名和密码

    1.在arc下系统提示使用__bridge   http://www.cnblogs.com/zzltjnh/p/3885012.html 参考文档:http://blog.csdn.net/jerr ...

  2. python - 数据驱动测试 - ddt

    # -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: study_ddt.py @ide: PyCharm Communit ...

  3. java面试题之HashMap和TreeMap的区别

    HashMap和TreeMap的区别 相同点: 都是以key和value的形式存储: key不可以重复: 都是线程不安全的: 不同点: HashMap的key可以为空 TreeMap的key值是有序的 ...

  4. window maven安装(六)

    Maven 实战系列之在Windows上安装Maven Maven是一个优秀的构建工具(类似于 Ant, 但比 Ant 更加方便使用),能帮助我们自动化构建过程,从清理.编译.测试到生成报告,再到打包 ...

  5. springboot获取getBean方法以及ApplicationContext空指针问题解决

    创建获取ApplicationContext工具类: package com.performancetest.common.utils; import org.springframework.bean ...

  6. python 序列化之pickle模块 json模块

    一 pickle import pickle s='dd' print(pickle.dumps(s)) 输出: b'\x80\x03X\x02\x00\x00\x00ddq\x00.' pickle ...

  7. 主机ping不通虚拟机,但是虚拟机能ping通主机

    一.虚拟机网络连接方式选择Nat 二. 关闭Linux防火墙命令:service iptables stop / service firewalld stop 查看Linux防火墙状态命令:servi ...

  8. 大规模SOA系统中的分布事务思考

    首先是不建议采用XA两阶段提交方式去处理分布式事务,要知道要能够支持XA分布式事务,必须是要实现XA规范才可以,而Service本身是无状态的,如果这样去做了等于是把Service内部的东西暴露了出去 ...

  9. [LeetCode] Minimum Window Substring 散列映射问题

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  10. 【MFC】 点击不同的按钮后在界面同一位置显示不同的对话框内容(转)

    原文转自 http://bbs.csdn.net/topics/391039432 如图类似Tab控件的功能    但Tab控件按钮是固定的上下左右  不方便     所以想自己重新做个这种   我M ...