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

最新文章

  1. PHP面向对象_重载新的方法(parent::)
  2. Oracle自动备份脚本(网上找到的资料)
  3. linux下mysql的忘记root密码的解决办法
  4. atitit.获取北京时间CST 功能api总结 O7
  5. 如何使用MVP模式搭建我们的Android应用?
  6. c#集合解析
  7. json前后台传值
  8. 自己动手写spring容器(2)
  9. Nginx location配置详细解释
  10. 换工作之后需要兼容ie8的我
  11. Netty 系列七(那些开箱即用的 ChannelHandler).
  12. 如何在Windows中通过Cygwin来使用Linux命令行
  13. 服务器重复发送SYN ACK 和 TCP_DEFER_ACCEPT设置
  14. vs2010 快捷键
  15. js 时间操作积累
  16. struts框架之总结OGNL表达式的特殊的符号
  17. Go RabbitMQ (一)
  18. Unity主线程和子线程跳转调用(2)
  19. bzoj 1006: [HNOI2008]神奇的国度 -- 弦图(最大势算法)
  20. React + Dva + Antd + Umi 概况

热门文章

  1. 光学字符识别OCR-3
  2. loj2020 「HNOI2017」礼物
  3. bzoj3039 joyoi1939 玉蟾宫 悬线法
  4. webdriver高级应用- 禁止Chrome浏览器的PDF和Flash插件
  5. js时间格式化工具,时间戳格式化,字符串转时间戳
  6. rabbitmq exchange type
  7. iOS学习笔记49-Swift(九)访问控制
  8. 【Luogu】P3705新生舞会(费用流+分数规划+二分答案)
  9. HDU-2768 Cat vs. Dog
  10. BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树