Python应该为可变的默认参数发出警告吗? [英] Should Python raise a warning for mutable default arguments?
问题描述
有时似乎只有一天没有一些新手,或者没有 -
所以新手,对可变的函数的行为感到困惑
默认参数。一个线程最终,痛苦地消失了,而不是另一个线程开始消失。
我建议Python应该提出警告.RuntimeWarning(或类似的) ?)
当使用由列表组成的默认参数定义函数时,
dict或set。 (这并不是所有可能的
可变类型的详尽列表,但是作为最常见的可能会绊倒
新手。)警告应该参考到相关的常见问题或
文档部分。
人们怎么想?
-
史蒂文
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully, fade
away than another one starts up.
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.
What do people think?
--
Steven
推荐答案
Steven D''Aprano schrieb:
Steven D''Aprano schrieb:
有时它似乎只有一天没有一些新手,或者不是这样的新手,对可变的
默认参数的函数的行为感到困惑。一个线程最终,痛苦地消失了,而不是另一个线程开始消失。
我建议Python应该提出警告.RuntimeWarning(或类似的) ?)
当使用由列表组成的默认参数定义函数时,
dict或set。 (这并不是所有可能的
可变类型的详尽列表,但是作为最常见的可能会绊倒
新手。)警告应该参考到相关的常见问题或
文档部分。
Sometimes it seems that barely a day goes by without some newbie, or not-
so-newbie, getting confused by the behaviour of functions with mutable
default arguments. No sooner does one thread finally, and painfully, fade
away than another one starts up.
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.
我刚建议在过多的
线程之一的文档增强...所以我肯定会+1这个领域的增强。
Diez
I just suggested a documentation enhancement in one of the plethora of
threads... so I''m certainly +1 for any enhancement in this area.
Diez
Steven D''Aprano写道:
Steven D''Aprano wrote:
我建议Python应该引发warnings.RuntimeWarning(或者类似的?)
当一个函数被定义为包含一个列表的默认参数时,
字典或集。 (这并不是所有可能的
可变类型的详尽列表,但是作为最常见的可能会绊倒
新手。)警告应该参考到相关的常见问题或
文档部分。
人们怎么想?
I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.
What do people think?
我没有看到你提案的机会。你怎么会发现
可变对象?自定义类型可以是可变的也可以是不可变的。
Christian
I don''t see a chance for your proposal. How are you going to detect
mutable objects? Custom types can be mutable as well as immutable.
Christian
Christian Heimes写道:
Christian Heimes wrote:
Steven D''Aprano写道:
Steven D''Aprano wrote:
>我建议Python应该引发警告.RuntimeWarning(或类似?当使用由列表,字典或集合组成的默认参数定义函数时。
(这并不是所有可能的可变类型的详尽列表,但是作为我期望的最常见的类型将会绊倒
新手。)警告应参考相关的常见问题或部分。
docs。
人们怎么想?
>I suggest that Python should raise warnings.RuntimeWarning (or similar?)
when a function is defined with a default argument consisting of a list,
dict or set. (This is not meant as an exhaustive list of all possible
mutable types, but as the most common ones that I expect will trip up
newbies.) The warning should refer to the relevant FAQ or section in the
docs.
What do people think?
-0来自我。我宁愿在教程中突出显示它,
部分五个最常见的陷阱或类似的东西。
-0 from me. I''d rather feature it more prominently in the tutorial, a
section "The five most common pitfalls" or something like that.
我没有看到你提议的机会。你怎么会发现
可变对象?自定义类型可以是可变的也可以是不可变的。
I don''t see a chance for your proposal. How are you going to detect
mutable objects? Custom types can be mutable as well as immutable.
在编译时检查列表文字会检查90%的情况。
警告毕竟是针对新手的。当他们试图导入其他有意使用
可变默认值的代码时,它可能仍然是一个混乱的来源。
彼得
A check at compilation time for list literals would catch 90 % of the cases.
The warning would be targeted at newbies after all. It might still be a
source of confusion when they try to import someone else''s code that uses
mutable defaults intentionally.
Peter
这篇关于Python应该为可变的默认参数发出警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!