Python函数中的可选参数及其默认值[重复]

时间:2022-01-23 10:50:57

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

可能重复:Python中的“最小惊讶”:可变默认参数

I'm kind of confused about how optional parameters work in Python functions/methods.

我对Python函数/方法中可选参数的工作原理感到困惑。

I have the following code block:

我有以下代码块:

>>> def F(a, b=[]):
...     b.append(a)
...     return b
...
>>> F(0)
[0]
>>> F(1)
[0, 1]
>>>

Why F(1) returns [0, 1] and not [1]?

为什么F(1)返回[0,1]而不是[1]?

I mean, what is happening inside?

我的意思是,里面发生了什么?

2 个解决方案

#1


39  

Good doc from PyCon a couple years back - Default parameter values explained. But basically, since lists are mutable objects, and keyword arguments are evaluated at function definition time, every time you call the function, you get the same default value.

几年前来自PyCon的好文档 - 解释了默认参数值。但基本上,由于列表是可变对象,并且在函数定义时评估关键字参数,因此每次调用该函数时,都会得到相同的默认值。

The right way to do this would be:

正确的方法是:

def F(a, b=None):
    if b is None:
        b = []
    b.append(a)
    return b

#2


9  

Default parameters are, quite intuitively, somewhat like member variables on the function object.

直观地说,默认参数有点像函数对象上的成员变量。

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 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.

执行函数定义时,将评估默认参数值。这意味着当定义函数时,表达式被计算一次,并且每次调用使用相同的“预先计算”值。这对于理解默认参数是可变对象(例如列表或字典)时尤其重要:如果函数修改对象(例如,通过将项附加到列表),则默认值实际上被修改。

http://docs.python.org/reference/compound_stmts.html#function

Lists are a mutable objects; you can change their contents. The correct way to get a default list (or dictionary, or set) is to create it at run time instead, inside the function:

列表是一个可变对象;你可以改变他们的内容。获取默认列表(或字典或集合)的正确方法是在运行时创建它,而不是在函数内部:

def good_append(new_item, a_list=None):
    if a_list is None:
        a_list = []
    a_list.append(new_item)
    return a_list

#1


39  

Good doc from PyCon a couple years back - Default parameter values explained. But basically, since lists are mutable objects, and keyword arguments are evaluated at function definition time, every time you call the function, you get the same default value.

几年前来自PyCon的好文档 - 解释了默认参数值。但基本上,由于列表是可变对象,并且在函数定义时评估关键字参数,因此每次调用该函数时,都会得到相同的默认值。

The right way to do this would be:

正确的方法是:

def F(a, b=None):
    if b is None:
        b = []
    b.append(a)
    return b

#2


9  

Default parameters are, quite intuitively, somewhat like member variables on the function object.

直观地说,默认参数有点像函数对象上的成员变量。

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 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.

执行函数定义时,将评估默认参数值。这意味着当定义函数时,表达式被计算一次,并且每次调用使用相同的“预先计算”值。这对于理解默认参数是可变对象(例如列表或字典)时尤其重要:如果函数修改对象(例如,通过将项附加到列表),则默认值实际上被修改。

http://docs.python.org/reference/compound_stmts.html#function

Lists are a mutable objects; you can change their contents. The correct way to get a default list (or dictionary, or set) is to create it at run time instead, inside the function:

列表是一个可变对象;你可以改变他们的内容。获取默认列表(或字典或集合)的正确方法是在运行时创建它,而不是在函数内部:

def good_append(new_item, a_list=None):
    if a_list is None:
        a_list = []
    a_list.append(new_item)
    return a_list