如何避免由 Python 的早期绑定默认参数(例如可变默认参数“记住”旧数据)引起的问题?
有时,有一个空列表的默认参数似乎很自然。 但是,Python 在这些情况下会产生意外行为。
例如,考虑这个函数:
def my_func(working_list=[]):
working_list.append("a")
print(working_list)
第一次调用时,默认值将起作用,但之后的调用将更新现有列表(每次调用一个“a”
)并打印更新的版本。
如何修复该函数,以便在没有显式参数的情况下重复调用该函数时,每次都使用新的空列表?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
文档说你应该使用
None
作为默认且显式在正文中对其进行测试的函数。The docs say you should use
None
as the default and explicitly test for it in the body of the function.其他答案已经提供了所要求的直接解决方案,但是,由于这对于新的 Python 程序员来说是一个非常常见的陷阱,因此值得添加解释为什么 Python 会以这种方式运行,这在 Python 搭便车指南位于可变默认参数:
Other answers have already already provided the direct solutions as asked for, however, since this is a very common pitfall for new Python programmers, it's worth adding the explanation of why Python behaves this way, which is nicely summarized in The Hitchhikers Guide to Python under Mutable Default Arguments:
在这种情况下并不重要,但您可以使用对象标识来测试 None:
您还可以利用布尔运算符 or 在 python 中的定义方式:
尽管如果调用者给您一个空列表(其中算作 false)作为工作列表,并期望您的函数修改他给它的列表。
Not that it matters in this case, but you can use object identity to test for None:
You could also take advantage of how the boolean operator or is defined in python:
Though this will behave unexpectedly if the caller gives you an empty list (which counts as false) as working_list and expects your function to modify the list he gave it.
如果函数的目的是修改作为
working_list
传递的参数,请参阅HenryR的答案(=None,检查内部是否为None)。但是,如果您不打算改变参数,只需将其用作列表的起点,您可以简单地复制它:(
或者在这个简单的情况下,只需
print running_list + ["a"]
但我想这只是一个玩具示例)一般来说,改变你的参数在 Python 中是不好的风格。 唯一完全期望改变对象的函数是对象的方法。 改变可选参数的情况更加罕见——仅在某些调用中发生的副作用真的是最好的接口吗?
如果您按照“输出参数”的 C 习惯执行此操作,则完全没有必要 - 您始终可以将多个值作为元组返回。
如果您这样做是为了有效地构建一长串结果而不构建中间列表,请考虑将其编写为生成器并在调用它时使用
result_list.extend(myFunc())
。 这样,您的调用约定就保持非常干净。经常改变可选参数的一种模式是递归函数中隐藏的“memo”参数:
If the intent of the function is to modify the parameter passed as
working_list
, see HenryR's answer (=None, check for None inside).But if you didn't intend to mutate the argument, just use it as starting point for a list, you can simply copy it:
(or in this simple case just
print starting_list + ["a"]
but I guess that was just a toy example)In general, mutating your arguments is bad style in Python. The only functions that are fully expected to mutate an object are methods of the object. It's even rarer to mutate an optional argument — is a side effect that happens only in some calls really the best interface?
If you do it from the C habit of "output arguments", that's completely unnecessary - you can always return multiple values as a tuple.
If you do this to efficiently build a long list of results without building intermediate lists, consider writing it as a generator and using
result_list.extend(myFunc())
when you are calling it. This way your calling conventions remains very clean.One pattern where mutating an optional arg is frequently done is a hidden "memo" arg in recursive functions:
我可能偏离主题,但请记住,如果您只想传递可变数量的参数,Pythonic 方法是传递元组
*args
或字典**kargs. 这些是可选的,并且比语法
myFunc([1, 2, 3])
更好。如果你想传递一个元组:
如果你想传递一个字典:
I might be off-topic, but remember that if you just want to pass a variable number of arguments, the pythonic way is to pass a tuple
*args
or a dictionary**kargs
. These are optional and are better than the syntaxmyFunc([1, 2, 3])
.If you want to pass a tuple:
If you want to pass a dictionary:
回顾一下
Python 提前计算参数/参数的默认值; 他们是“早期束缚”的。 这可能会以几种不同的方式导致问题。 例如:
然而,问题最常见的表现方式是当函数的参数可变(例如
列表
)时,并且在函数的内部发生变化。代码。 发生这种情况时,更改将被“记住”,从而在后续调用中“看到”:因为
a_list
是提前创建的,所以每次调用使用默认值将使用相同的列表对象,该对象会在每次调用时进行修改,并附加另一个1
值。这是一个有意识的设计决策,可以在某些情况下会被利用 - 尽管通常有更好的方法来解决其他问题。 (考虑使用
functools.cache
或functools.lru_cache
进行记忆,并且functools.partial 绑定函数参数。)这也意味着实例的方法无法使用实例的属性作为默认值:在确定默认值时,
self
不在作用域内,并且该实例无论如何也不存在:(类
Example
also 尚不存在,名称Example
也also 不在范围;因此,类属性在这里也不起作用,即使我们不关心可变性问题。)解决方案
使用
None
作为哨兵值的通常是标准的考虑惯用的方法是使用
None
作为默认值,并显式检查该值并在函数的逻辑中替换它。 因此:之所以有效,是因为代码
a_list = []
运行(如果需要)函数被调用,而不是提前 - 因此,它每次都会创建一个新的空列表。 因此,这种方法也可以解决datetime.now()
问题。 这确实意味着该函数不能将None
值用于其他目的; 但是,这不会在普通代码中引起问题。简单地避免可变默认值
如果不需要修改参数来实现函数的逻辑,因为 命令查询分离,最好不这样做。
根据这个论点,
append_one_and_return
的设计一开始就很糟糕:由于目的是显示输入的某些修改版本,因此它不应该实际修改调用者的变量,但只是创建一个新对象用于显示目的。 这允许使用不可变对象(例如元组)作为默认值。 因此:即使明确提供了输入,这种方式也将避免修改输入:
无需参数即可正常工作,甚至重复:
并且它获得了一定的灵活性:
PEP 671
PEP 671 建议向 Python 引入新语法,允许显式后期绑定参数的默认值。 提议的语法是:
然而,虽然本 PEP 草案提议在 Python 3.12 中引入该功能,但这并没有发生,并且尚无此类语法。 最近对此想法进行了一些讨论,但是Python 在不久的将来似乎不太可能支持它。
Recap
Python evaluates default values for arguments/parameters ahead of time; they are "early-bound". This can cause problems in a few different ways. For example:
The most common way the problem manifests, however, is when the argument to the function is mutable (for example, a
list
), and gets mutated within the function's code. When this happens, changes will be "remembered", and thus "seen" on subsequent calls:Because
a_list
was created ahead of time, every call to the function that uses the default value will use the same list object, which gets modified on each call, appending another1
value.This is a conscious design decision that can be exploited in some circumstances - although there are often better ways to solve those other problems. (Consider using
functools.cache
orfunctools.lru_cache
for memoization, and functools.partial to bind function arguments.)This also implies that methods of an instance cannot use an attribute of the instance as a default: at the time that the default value is determined,
self
is not in scope, and the instance does not exist anyway:(The class
Example
also doesn't exist yet, and the nameExample
is also not in scope; therefore, class attributes will also not work here, even if we don't care about the mutability issue.)Solutions
Using
None
as a sentinel valueThe standard, generally-considered-idiomatic approach is to use
None
as the default value, and explicitly check for this value and replace it in the function's logic. Thus:This works because the code
a_list = []
runs (if needed) when the function is called, not ahead of time - thus, it creates a new empty list every time. Therefore, this approach can also solve thedatetime.now()
issue. It does mean that the function can't use aNone
value for other purposes; however, this should not cause a problem in ordinary code.Simply avoiding mutable defaults
If it is not necessary to modify the argument in order to implement the function's logic, because of the principle of command-query separation, it would be better to just not do that.
By this argument,
append_one_and_return
is poorly designed to begin with: since the purpose is to display some modified version of the input, it should not also actually modify the caller's variable, but instead just create a new object for display purposes. This allows for using an immutable object, such as a tuple, for the default value. Thus:This way will avoid modifying the input even when that input is explicitly provided:
It works fine without an argument, even repeatedly:
And it has gained some flexibility:
PEP 671
PEP 671 proposes to introduce new syntax to Python that would allow for explicit late binding of a parameter's default value. The proposed syntax is:
However, while this draft PEP proposed to introduce the feature in Python 3.12, that did not happen, and no such syntax is yet available. There has been some more recent discussion of the idea, but it seems unlikely to be supported by Python in the near future.
引用自 https://docs.python.org/3/reference/ compound_stmts.html#function-definitions
Quote from https://docs.python.org/3/reference/compound_stmts.html#function-definitions
已经提供了良好且正确的答案。 我只是想提供另一种语法来编写您想要执行的操作,例如,当您想要创建一个具有默认空列表的类时,我发现这种语法更漂亮:
此代码片段使用了 if else 运算符语法。 我特别喜欢它,因为它是一个简洁的小单行,没有冒号等,读起来几乎就像一个正常的英语句子。 :)
在你的情况下你可以写
There have already been good and correct answers provided. I just wanted to give another syntax to write what you want to do which I find more beautiful when you for instance want to create a class with default empty lists:
This snippet makes use of the if else operator syntax. I like it especially because it's a neat little one-liner without colons, etc. involved and it nearly reads like a normal English sentence. :)
In your case you could write
也许最简单的事情就是在脚本中创建列表或元组的副本。 这避免了检查的需要。 例如,
Perhaps the simplest thing of all is to just create a copy of the list or tuple within the script. This avoids the need for checking. For example,
我采用了 UCSC 扩展类
Python for Programmer
这是正确的: def Fn(data = []):
回答:
I took the UCSC extension class
Python for programmer
Which is true of: def Fn(data = []):
Answer: