在多年开发和教学 Python 的过程中,有一个 bug,出现的频率非常高,经常有人会踩坑:
person = {'name': '', 'id': 0} team = [] for i in range(3): x = person x['id'] = i team.append(x) team[0]['name'] = 'Jack' team[1]['name'] = 'Pony' team[2]['name'] = 'Crossin' print(team[1])
如果觉得这个输出结果是 {‘name’: ‘Pony’, ‘id’: 1} ,那么就掉进这个坑里了。
如果真的运行一下就会发现,team里面三个都是 {‘name’: ‘Crossin’, ‘id’: 2} , 而且,如果之后再更改任何一个的 name 或 id 属性,另外两个也会跟着变。
那么为什么呢?
在 Python 中,要把变量想象成一个标签,而不是一个容器!
什么意思?在某些语言中,变量确实像一个“容器”,你定义了某种类型的变量,就给你分配好这个容器,之后你给变量赋值,就像是往容器里装入不同的内容,但容器还是那个容器,不会变。你创建3个容器,赋给一样的值,他们也还是3个独立的容器。
但在 Python 中,这个理解是错误的!
Python 中的变量像是一个“标签”,你给一个变量赋值,就是把这个标签贴在一个对象上;重新赋值,就是撕下标签帖到另一个对象上。
给3个变量赋给一样的值,就相当于把3个标签贴在同一个对象上。
用例子来说明:
a = 1 b = 2 c = 1 # 再次赋值 a = b
示意图:

通过输出 id(相当于内存地址)也可以说明这点:

在赋值之后,其实并不是 a 的值发生了变化,而是 a 的地址发生了变化。
理解了这点之后,你就会明白2件事:
- 当你给一个变量重新赋值之后,它就不再是之前的那个变量,所有的操作不会再影响到之前的变量上
- 当你给多个变量赋值相同的变量,它们其实都是同一个,只要改动其中之一,其他的也会跟着变化。(注意,是改动而非重新赋值,比如修改对象属性)
这个原理,在有关函数的参数传递、拷贝对象时都会涉及到。
因此,回到最初的问题,三个重新赋值其实本质是三个共用同一地址。
本文转载自微信公众号 Crossin的编程教室 ,为以后避免踩坑而记录。
有时候一天长的像一生,
有时候一生短的像一天。
世界上没有两个相同的一天,
但每一天都适合开始,
适合离别,适合爱。
——周一周二


评论
354028 42988I agree with most of your points, but a few need to be discussed further, I will hold a small speak with my partners and perhaps I will appear for you some suggestion soon. 370860
663675 418325I got what you intend, saved to fav, extremely nice web site . 506486
372116 846590Excellent weblog, Im going to spend more time reading about this subject 397744
756491 52030You produced some decent points there. I looked online for that issue and identified many people goes coupled with with all your web site. 426918
799097 192311Admiring the time and energy you put into your weblog and in depth data you offer. It is very good to come across a weblog every once in a even though that isnt exactly the same old rehashed material. Wonderful read! Ive bookmarked your internet site and Im adding your RSS feeds to my Google account. 202485
173319 609055Enjoyed examining this, very good stuff, thanks . 314984
286188 520161When I came over to this post I can only look at part of it, is this my net browser or the internet website? Really should I reboot? 960841
982626 205716Dude.. My group is not considerably into searching at, but somehow I acquired to read several articles on your weblog. Its fantastic how fascinating its for me to pay a visit to you fairly often. 426502
818377 360064 You created some decent points there. I looked on the internet for the problem and found most individuals will go along with along with your website. 855543
639387 853202A thoughtful insight and concepts I will use on my weblog. Youve certainly spent lots of time on this. Thank you! 863050
733783 885075You must consider starting an e-mail list. It would take your web site to its potential. 84464
723465 797558Oh my goodness! an superb article dude. Thank you Nonetheless Im experiencing difficulty with ur rss . Do not know why Cannot register for it. Could there be any person getting identical rss difficulty? Anybody who knows kindly respond. Thnkx 763063
933457 213709cool thanks for reis posting! btw are there feeds to your blog? Id adore to add them to my reader 592625
109352 998939Whats up very nice site!!Man .. Beautiful .. Superb. 776634