Default Parameter Values in Python

Python’s handling of default parameter values is one of a few things that tends to trip up most new Python programmers (but usually only once).

What causes the confusion is the behaviour you get when you use a “mutable” object as a default value; that is, a value that can be modified in place, like a list or a dictionary.

An example:

>>> def function(data=[]):
...     data.append(1)
...     return data
...
>>> function()
[1]
>>> function()
[1, 1]
>>> function()
[1, 1, 1]

As you can see, the list keeps getting longer and longer. If you look at the list identity, you’ll see that the function keeps returning the same object:

>>> id(function())
12516768
>>> id(function())
12516768
>>> id(function())
12516768

The reason is simple: the function keeps using the same object, in each call. The modifications we make are “sticky”.

Why does this happen? #

Default parameter values are always evaluated when, and only when, the “def” statement they belong to is executed; see:

http://docs.python.org/ref/function.html (dead link)

for the relevant section in the Language Reference.

Also note that “def” is an executable statement in Python, and that default arguments are evaluated in the “def” statement’s environment. If you execute “def” multiple times, it’ll create a new function object (with freshly calculated default values) each time. We’ll see examples of this below.

What to do instead? #

The workaround is, as others have mentioned, to use a placeholder value instead of modifying the default value. None is a common value:

def myfunc(value=None):
    if value is None:
        value = []
    # modify value here

If you need to handle arbitrary objects (including None), you can use a sentinel object:

sentinel = object()

def myfunc(value=sentinel):
    if value is sentinel:
        value = expression
    # use/modify value here

In older code, written before “object” was introduced, you sometimes see things like

sentinel = [‘placeholder‘]

used to create a non-false object with a unique identity; [] creates a new list every time it is evaluated.

Valid uses for mutable defaults #

Finally, it should be noted that more advanced Python code often uses this mechanism to its advantage; for example, if you create a bunch of UI buttons in a loop, you might try something like:

for i in range(10):
    def callback():
        print "clicked button", i
    UI.Button("button %s" % i, callback)

only to find that all callbacks print the same value (most likely 9, in this case). The reason for this is that Python’s nested scopes bind to variables, not object values, so all callback instances will see the current (=last) value of the “i” variable. To fix this, use explicit binding:

for i in range(10):
    def callback(i=i):
        print "clicked button", i
    UI.Button("button %s" % i, callback)

The “i=i” part binds the parameter “i” (a local variable) to the current value of the outer variable “i”.

Two other uses are local caches/memoization; e.g.

(It happened to me in one of the first Python programs I ever wrote, and it took several years before we spotted the (non-critical) bug, when someone looked a bit more carefully at the contents of a property file, and wondered what all those things were doing there…)

def calculate(a, b, c, memo={}):
    try:
        value = memo[a, b, c] # return already calculated value
    except KeyError:
        value = heavy_calculation(a, b, c)
        memo[a, b, c] = value # update the memo dictionary
    return value

(this is especially nice for certain kinds of recursive algorithms)

and, for highly optimized code, local rebinding of global names:

import math

def this_one_must_be_fast(x, sin=math.sin, cos=math.cos):
    ...

How does this work, in detail? #

When Python executes a “def” statement, it takes some ready-made pieces (including the compiled code for the function body and the current namespace), and creates a new function object. When it does this, it also evaluates the default values.

The various components are available as attributes on the function object; using the function we used above:

>>> function.func_name
‘function‘
>>> function.func_code
<code object function at 00BEC770, file "<stdin>", line 1>
>>> function.func_defaults
([1, 1, 1],)
>>> function.func_globals
{‘function‘: <function function at 0x00BF1C30>,
‘__builtins__‘: <module ‘__builtin__‘ (built-in)>,
‘__name__‘: ‘__main__‘, ‘__doc__‘: None}

Since you can access the defaults, you can also modify them:

>>> function.func_defaults[0][:] = []
>>> function()
[1]
>>> function.func_defaults
([1],)

However, this is not exactly something I’d recommend for regular use…

Another way to reset the defaults is to simply re-execute the same “def” statement. Python will then create a new binding to the code object, evaluate the defaults, and assign the function object to the same variable as before. But again, only do that if you know exactly what you’re doing.

And yes, if you happen to have the pieces but not the function, you can use thefunction class in the new module to create your own function object.

时间: 2024-09-30 21:35:09

Default Parameter Values in Python的相关文章

ES6基础——默认参数 Default Parameter Values

在ES6里面我们可以去给函数接受的参数制定默认的值,那么在执行这个函数的时候,如果不指定函数的参数的值的话,就会使用这些参数默认的值 例子: function breakfast(dessert = 'cake',drink = 'tea'){ return `${dessert} ${drink} ` } console.log(breakfast()); //cake tea 这里没有给函数设定参数,所以使用了默认的值:如果设定了参数,则会使用设定的参数值. 例子: function bre

JavaScript函数的默认参数(default parameter)

JavaScript函数的默认参数(default parameter) js函数参数的默认值都是undefined, ES5里,不支持直接在形参里写默认值.所以,要设置默认值,就要检测参数是否为undefined,按需求赋值. function multiply(a, b) { b = typeof b !== 'undefined' ? b : 1; return a*b; } multiply(5); // 5 multiply(5, 0); // 0 上面是MDN的相关例子,是比较严谨的

[转]如何像Python高手(Pythonista)一样编程

本文转自:http://xianglong.me/article/how-to-code-like-a-pythonista-idiomatic-python 最近在网上看到一篇介绍Pythonic编程的文章:Code Like a Pythonista: Idiomatic Python,其实作者在2006的PyCon会议后就写了这篇文章,写这篇文章的主要原因是作者发现很多有经验的Pythoner写出的代码不够Pythonic.我觉得这篇文章很不错,所以将它用中文写了下来(不是逐字的翻译,中间

Python函数参数默认值的陷阱和原理深究(转)

add by zhj: 在Python文档中清楚的说明了默认参数是怎么工作的,如下 "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

程序员必知的Python陷阱与缺陷列表

本文关注的主要是python陷阱,具体而言,是指CPython,而且除非特别说明,所有代码示例都是在python2.7运行的. 我个人对陷阱的定义是这样的:代码看起来可以工作,但不是以你“想当然“”的方式.如果一段代码直接出错,抛出了异常,我不认为这是陷阱.比如,Python程序员应该都遇到过的“UnboundLocalError", 示例: >>> a=1 >>> def func(): ...     a+=1 ...     print a ... &g

如何像Python高手(Pythonista)一样编程

最近在网上看到一篇介绍Pythonic编程的文章:Code Like a Pythonista: Idiomatic Python,其实作者在2006的PyCon会议后就写了这篇文章,写这篇文章的主要原因是作者发现很多有经验的Pythoner写出的代码不够Pythonic.我觉得这篇文章很不错,所以将它用中文写了下来(不是逐字的翻译,中间加了一些自己的理解),分享给大家.另:由于本人平时时间有限,这篇文章翻译了比较长的时间,如果你发现了什么不对的地方,欢迎指出.. 一.Python之禅(The

Python——Code Like a Pythonista: Idiomatic Python

Code Like a Pythonista: Idiomatic Python 如果你有C++基础,那学习另一门语言会相对容易.因为C++即面向过程,又面向对象.它很底层,能像C一样访问机器:它也很高级,有模板.STL 等.如果认真读过<深入C++对象模型>,我想其它语言不会比这个更复杂.你对C++了解的越深,你用其它语言写出的代码就越像C++,这样就体现不出新 语言的价值了.所以,学习新语言要抛弃原有的潜意识,包括代码风格. 1. The Zen of Python (1) 漂亮比丑陋好

A few things to remember while coding in Python.

A few things to remember while coding in Python. - 17 May 2012 - UPDATE: There has been much discussion in Hacker News about this article. A few corrections from it. Zen of Python Learning the culture that surrounds a language brings you one step clo

SSIS Parameter用法

Parameters能够在Project Deployment Model下使用,不能在Package Deployment Model使用.在Package Deployment Model下,使用Package Configurations来传递属性值:在Project Deployment Model下,使用Parameters来传递值. 1,Parameters and Package Deployment Model In general, if you are deploying a