> For the complete documentation index, see [llms.txt](https://l1nwatch.gitbook.io/interview_exercise/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://l1nwatch.gitbook.io/interview_exercise/stackoverflow-about-python/bian-hua-de-mo-ren-can-shu.md).

# 变化的默认参数

存在一个奇怪的现象：

```python
def foo(a=[]):
    a.append(5)
    return a
>>> foo()
[5]
>>> foo()
[5, 5]
>>> foo()
[5, 5, 5]
>>> foo()
[5, 5, 5, 5]
>>> foo()
```

曾经被称为动态设计缺陷，然而这个问题应当有更深层次的解释。

Python 中的函数是最高等级的对象，而不仅仅是一小段代码。一个函数是一个被它自己定义而执行的对象，默认参数是一种“成员数据”，所以它们的状态和其他对象一样，会随着每一次调用而改变。

更详细的内容参考：[Python中的默认参数](http://effbot.org/zone/default-values.htm)
