意外的Python行为 [英] Unexpected Python Behavior
问题描述
我第一次被Python咬了。以下代码
产生结果:
False
真实
当我最初预期结果:
错误
错误
我花了一段时间来计算出默认关键字参数值
可能只被评估一次,这导致空的dict在各个类之间共享... / $
这肯定是新手python编码器应该看的东西出去!
Simon W.
--- snip-here ---
class X(object):
def __init __(self,d = {}):
self.d = d
a = X()
b = X()
打印a是b
打印广告是bd
--- snip-here ---
For the first time, I have been bitten by Python. The below code
produces the results:
False
True
when I initially expected the results:
False
False
It took me a while to work out that default keyword argument values
are likely only evaluated once, which caused the empty dict to be
shared across classes...
It certainly something newbie python coders should look out for!
Simon W.
---snip-here---
class X(object):
def __init__(self, d={}):
self.d = d
a = X()
b = X()
print a is b
print a.d is b.d
---snip-here---
推荐答案
Simon Wittber写道:
Simon Wittber wrote:
我花了一段时间来确定默认关键字参数值
可能只评估一次,这导致空字典在各个班级共享...
一些相关链接:
http://www.python.org/doc/faq/genera .. .etween-objects
http:// docs.python.org/tut/node6.htm...00000000000000
http://docs.python.org/ref/function.html
肯定是新手python程序员需要注意的事情!
It took me a while to work out that default keyword argument values
are likely only evaluated once, which caused the empty dict to be
shared across classes...
some relevant links:
http://www.python.org/doc/faq/genera...etween-objects
http://docs.python.org/tut/node6.htm...00000000000000
http://docs.python.org/ref/function.html
It certainly something newbie python coders should look out for!
它是众所周知的你只会这样做一次错误。这是一件好事,
,因为当你明白为什么会发生这种情况时,你已经学到了很多关于
如何def的信息。和对象在Python中工作...
< / F>
it''s a well-known "you''ll only do this once" mistake. which is a good thing,
because when you understand why this happens, you have learned a lot about
how "def" and objects work in Python...
</F>
Fredrik Lundh < FR ***** @ pythonware.com>在消息中写道>
"Fredrik Lundh" <fr*****@pythonware.com> wrote in message >
它是一个众所周知的你只会这样做一次错误。这是一个很好的
it''s a well-known "you''ll only do this once" mistake. which is a good
的事情,
"由于这个功能,不使用mutable是一个很好的编程习惯
对象作为默认值。 -
http://www.python.org/doc/faq/genera...etween-objects
是否讨论过它是否会是在这个案例中,好主意在
发出警告?令我感到震惊的是,一个警告不会打扰经验丰富的程序员,因为它很容易避免使用可变的默认值(几乎不需要修改代码就可以了。)使用可变的默认值)。我想象它
也使代码更具可读性。
David Pokorny
thing,
"Because of this feature, it is good programming practice to not use mutable
objects as default values." --
http://www.python.org/doc/faq/genera...etween-objects
Has it been discussed whether it would be a good idea to issue a warning in
this case? It strikes me that a warning wouldn''t bother veteran programmers,
since it is really easy to avoid using a mutable default value (nearly
trivial to modify code that does use mutable default values). I''d imagine it
makes code more readable too.
David Pokorny
David Pokorny写道:
David Pokorny wrote:
"由于这个特性,不使用可变的
对象作为默认值是一种很好的编程习惯。 -
http://www.python.org/doc/faq/genera...etween-objects
是否讨论过发布是否是个好主意在这种情况下发出警告?
不是。
我觉得警告不会打扰资深程序员,因为它非常容易避免使用可变的默认值(几乎微不足道地修改使用可变默认值的代码)。我想它也会让代码更具可读性。
"Because of this feature, it is good programming practice to not use mutable
objects as default values." --
http://www.python.org/doc/faq/genera...etween-objects
Has it been discussed whether it would be a good idea to issue a warning in
this case?
It''s not.
It strikes me that a warning wouldn''t bother veteran programmers,
since it is really easy to avoid using a mutable default value (nearly
trivial to modify code that does use mutable default values). I''d imagine it
makes code more readable too.
我想你错过了这个功能的用处。返回您附带的
链接并阅读下一段,此功能非常有用。
-
Michael霍夫曼
I think you''re missing the usefulness of this feature. Go back to the
link you included and read the next paragraph, "This feature can be useful."
--
Michael Hoffman
这篇关于意外的Python行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!