python函数的默认参数只评估一次? [英] python function default parameter is evaluated only once?

查看:124
本文介绍了python函数的默认参数只评估一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名python初学者,阅读'python tutorial',它说如果我们有一个函数:

  def f( a,L = []):
L.append(a)
return L
print f(1)
print f(2)
print f(3) )

这将打印

  [1] 
[1,2]
[1,2,3]

因为默认值只计算一次,list是一个可变对象。我可以理解它。



它继续说,如果我们不希望在后续调用之间共享默认值,我们可以:

  def f(a,L = None):
如果L是None:#line 2
L = []
.append(a)
返回L
print f(1)
print f(2)
print f(3)



这将输出:

  [1 ] 
[2]
[3]

/ STRONG>?如何解释这一点。我们知道默认值只有一次>时才被评估,当我们调用f(2)时,L不是None,并且如果 >(在第2行)不能为真,所以L.append(a)== [1,2]。我可以猜想默认值是为了一些理由而重新评估的,但是什么是'某种原因',仅仅因为python解释器在L为None的情况下看到:L = []

解决方案

Python通过值将参数传递给函数;因此对于对象,传递的值是对对象的引用,而不是对象的新副本。



这以及以下官方文档的一部分是帮助的我更好地理解它(重点是我的):


当函数定义执行时,默认参数值被评估。 这意味着表达式会在函数定义时评估一次,并且每次调用都使用相同的预先计算值。这对于了解何时默认参数为可变对象,如列表或字典:如果函数修改对象(例如,通过将项目附加到列表中),则默认值将被有效修改。 [...]解决这个问题的方法是使用None作为默认值,并在函数体中明确地测试它[...]


$

如果您将参数的默认值定义为可变对象(例如 [] ),那么预先计算的值就是该对象的引用,因此每次调用该函数将始终引用相同的对象,然后可以跨多个然而,既然 None是一个不可变的内置类型,那么预先计算的缺省值 None 的值就是这样。因此,每次调用该函数时,参数都是 None



希望这有所帮助!我认为教程本来可以有更好的措辞,因为起初我也对此感到困惑。


I am a python beginner, reading 'python tutorial', it says if we have a function:

def f(a, L=[]):
     L.append(a)
     return L
print f(1)
print f(2)
print f(3)

This will print

[1]
[1, 2]
[1, 2, 3]

Because the default value is evaluated only once and list is a mutable object. I can understand it.

And it says continue, if we don't want the default to be shared between subsquent calls, we can:

def f(a, L=None):
   if L is None:           #line  2
       L = []            
   L.append(a)
   return L
print f(1)            
print f(2)
print f(3)

and this will output:

[1]
[2]
[3]

But why? How to explain this. We know default value is evaluated only once, and when we call f(2), L is not None and that if(in line 2) can not be true, so L.append(a) == [1, 2]. Could I guess the default value is evaluated again for some reason , but what is 'some reason', just because the python interpreter see if L is None: L = []

解决方案

Python passes parameters to functions by value; So for objects, the value passed is a reference to the object, not a new copy of the object.

That, along with the following part of the official docs is what helped me understand it better (emphasis mine):

Default parameter values are evaluated [...] when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. [...] A way around this is to use None as the default, and explicitly test for it in the body of the function [...]

Putting it all together:

If you define the default for a parameter to be a mutable object (such as []) then the "pre-computed" value is the reference to that object, so each call to the function will always reference the same object, which can then be mutated across multiple invocations of the function.

However, since None is an immutable built-in type, the "pre-computed" value for a default of None is simply that. So the parameter will be None each time you call the function.

Hopefully that helps! I do think that the tutorial could have had better wording, because I was also confused by that at first.

这篇关于python函数的默认参数只评估一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆