python中的陷阱(1)
d = {} 与 d.clear() 的区别
在工作中我想实现一个这样的效果:
在循环中把一个字典的不同状态值加入到list里面去,可是这个结果一致很异常。
看下面的问题代码:
In [1]: l = [] In [2]: p = {} In [3]: for i in xrange(5): ...: p.clear() ...: p[str(i)] = i ...: print p ...: l.append(p) ...: print l ...: ...: {'0': 0} [{'0': 0}] {'1': 1} [{'1': 1}, {'1': 1}] {'2': 2} [{'2': 2}, {'2': 2}, {'2': 2}] {'3': 3} [{'3': 3}, {'3': 3}, {'3': 3}, {'3': 3}] {'4': 4} [{'4': 4}, {'4': 4}, {'4': 4}, {'4': 4}, {'4': 4}]
发现了吗? l.append操作每次只是把对象的地址放入了队列里面进行保存,而p.clear() 这个函数不会创建新的字典,只是把原来p = {}创建的变量内容给清空了。我们每次放入l之中的都是p = {}创建的变量地址,所以导致l 里面的结果跟所想想的完全不一致。
作为对比,下面才是我们想要的结果。
In [6]: l = [] In [7]: for i in xrange(5): p = {} p[str(i)] = i print p l.append(p) print l ....: ....: {'0': 0} [{'0': 0}] {'1': 1} [{'0': 0}, {'1': 1}] {'2': 2} [{'0': 0}, {'1': 1}, {'2': 2}] {'3': 3} [{'0': 0}, {'1': 1}, {'2': 2}, {'3': 3}] {'4': 4} [{'0': 0}, {'1': 1}, {'2': 2}, {'3': 3}, {'4': 4}]
我们必须在for循环的开始就通过p = {} 来创建一个新的变量,跟之前的那个变量地址完全不能一样。这样才能让不同的字典加入到我们的队列里面
上面是我的猜测,是真的这样吗? 我们验证一下
In [20]: l = [] In [21]: for i in xrange(5): p = {} p[str(i)] = i print p l.append(p) print l p.clear() ....: ....: {'0': 0} [{'0': 0}] {'1': 1} [{}, {'1': 1}] {'2': 2} [{}, {}, {'2': 2}] {'3': 3} [{}, {}, {}, {'3': 3}] {'4': 4} [{}, {}, {}, {}, {'4': 4}]
看到了吗? clear()成员函数清空了p = {}产生的变量的内容,而列表里面仍然保存着这个空变量的地址,列表自己当然不知道了。。 所以很遗憾。。 我们什么都没得到。。 我应该在最后再打印下l , 不过其实已经没有必要了, 不是吗?
Feb 22, 2011 08:41:53 AM
呃,我一点都没觉得这是陷阱啊。你既然没创建新的字典,那 clear 之后当然还是原来的那个呗。对于 mutable 对象,字面值总是创建一个新的实例,所以 p = {} 是丢弃了旧的 p,而新生成了一个空字典起名为 p。一个(让新手觉得)更诡异的是,以下语句是等价的:
a = b = 3
a = 3; b = 3
但以下却不是
a = b = []
a = []; b = []
像这种情况教程里已经解释得很清楚了,理解后就没事了。
Jan 17, 2024 09:45:25 PM
오피타임's commitment to customer satisfaction is evident in every visit.
Jan 18, 2024 05:01:51 AM
제주출장마사지's aroma massage by the sea is simply magical.
Aug 26, 2024 04:55:26 PM
For a long time me & my friend were searching for informative blogs, but now I am on the right place guys, you have made a room in my heart!
Aug 26, 2024 05:07:37 PM
I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!
Aug 26, 2024 05:11:13 PM
of chance and strategy. Unlike traditional casino games, the Aviator Game involves a virtual plane that ascends
Aug 26, 2024 05:11:42 PM
Link exchange is nothing else except it is only placing the other person’s weblog link on your page at
Aug 26, 2024 05:23:57 PM
all the time i used to read smaller content that also clear their motive, and that is also happening with this paragraph which I am reading now.
Aug 26, 2024 05:25:38 PM
I really hope to see the same high-grade blog posts by you later on as well.
Aug 26, 2024 05:27:58 PM
An intriguing discussion is worth comment. There’s no doubt that that you need to publish more on this subject, it may not be a taboo matter but typically folks don’t speak about these issues. To the next! Cheers.
Aug 26, 2024 05:29:30 PM
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you weren’t too busy looking for attention
Aug 26, 2024 05:34:47 PM
I blog quite often and I genuinely appreciate your content. This article has truly peaked my interest. I will book mark your site and keep checking for new information about once a week. I subscribed to your RSS feed as well.
Aug 26, 2024 05:35:05 PM
they have a system that allows users to leave comments about the work workshop, which will allow to the platform mostbet improve own service.
Aug 26, 2024 05:36:11 PM
you loved this information and you would like to receive additional facts relating to signs you are struggling with cocaine addiction kindly check out our own web
Aug 26, 2024 05:36:27 PM
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
Aug 26, 2024 05:36:30 PM
what you wish for to say regarding this article, in my view its in fact remarkable for me.
Aug 26, 2024 05:37:34 PM
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Aug 26, 2024 05:37:42 PM
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you weren’t too busy looking for attention
Aug 26, 2024 05:37:52 PM
Discover the premier slot community guiding you to authentic, certified slot sites. Join us for access to original, verified slot platforms and exclusive offers. Our platform ensures a secure and genuine gaming experience for all members. Explore the best in slot entertainment with our trusted community.
Aug 26, 2024 05:38:08 PM
There are actually numerous details like that to take into consideration. That may be a great level to deliver up. I provide the ideas above as general inspiration however clearly there are questions just like the one you convey up the place crucial thing might be working in trustworthy good faith. I don?t know if best practices have emerged round things like that, however I’m positive that your job is clearly identified as a good game. Both girls and boys feel the impression of just a moment’s pleasure, for the remainder of their lives.
Aug 26, 2024 05:38:11 PM
Very good info. Lucky me I recently found your blog by chance (stumbleupon).
Aug 26, 2024 06:05:41 PM
This is a fabulous post I seen by virtue of offer it. It is genuinely what I expected to see look for in future you will continue subsequent to sharing such an extraordinary post.
Aug 26, 2024 06:22:26 PM
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me .. Thanks for all your help and wishing you all the success in your business
Aug 26, 2024 06:39:59 PM
This is my first time i visit here. I found so many helpful stuff in your website especially its discussion. From the tons of responses on your posts, I guess I am not the only one having all the enjoyment here! keep up the excellent work
Aug 26, 2024 07:12:36 PM
I would like to thank you for the efforts you’ve put in writing this site.
Aug 26, 2024 07:24:27 PM
Hi i am kavin, its my first occasion to commenting anywhere, when i read this piece of writing i thought i could also make comment due to this sensible post.