Default arguments are a helpful feature, but there is one situation where they can be surprisingly unhelpful. Using a mutable type (like a list or dictionary) as a default argument and then modifying that argument can lead to strange results. It's usually best to avoid using mutable default arguments: to see why, try the following code locally.

Consider this function which adds items to a todo list. Users can provide their own todo list, or add items to a default list:

def todo_list(new_task, base_list=['wake up']):
base_list.append(new_task)
return base_list

We can call the function like this:

>>> todo_list("check the mail")
['wake up', 'check the mail']

So if later on we call it again:

>>> todo_list("begin orbital transfer")

The result is actully:

['wake up', 'check the mail', 'begin orbital transfer']

The list object base_list is only created once: when the todo_list function is defined. Lists are mutable objects. This list object is used every time the function is called, it isn't redefined each time the function is called. Because todo_list appends an item to the list, base_list can get longer each time that todo_list is called.

[Python] Problem with Default Arguments的更多相关文章

  1. scala - multiple overloaded alternatives of method bar define default arguments

    同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...

  2. Python TypeError: not enough arguments for format string

    今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...

  3. 类模板成员函数默认值问题:an out-of-line definition of a member of a class template cannot have default arguments

    template <typename T> class A { ); }; template<typename T> ) { /* */ } 对于类似上文代码,VS编译器会报 ...

  4. Templates and Default Arguments

    Default parameters for templates in C++: Like function default arguments, templates can also have de ...

  5. Default arguments and virtual function

    Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...

  6. [Python] Pitfalls: About Default Parameter Values in Functions

    Today an interesting bug (pitfall) is found when I was trying debug someone's code. There is a funct ...

  7. Python TypeError: not all arguments converted during string formatting ——元组tuple(a)和(a,)的区别

    今天写程序,想输出一个array的shape,原程序为: print('shape of testUImatrix:%s\nStart to make testUImatrix...'%(testui ...

  8. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  9. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

随机推荐

  1. django笔记10 cookie整理

    感谢武沛齐老师 Alex老师 cookie 没有cookie所有的网站都登录不上 客户端浏览器上的一个文件 {'user':'ljc'} {"user":'zpt'} reques ...

  2. EntityFramework学习笔记1--安装

    1.新建项目 2.工具=>NuGet程序包管理器=>程序包管理控制器 3.PM> Install-Package EntityFramework 安装EF

  3. vue中使用滚动效果

    new Vue({ el: '#app', data: function data() { return { bottom: false, beers: [] }; }, watch: { botto ...

  4. GoldenGate 双向复制解决方案

    1 双向复制方案简介 在双向复制(Bidirectional)方案中,可以采用以下两种部署方式: 方式一:配置源和目标数据库可以同时保持Active 状态,同时进行应用系统的事务处理, 此时需由应用系 ...

  5. 我所理解的monad(1):半群(semigroup)与幺半群(monoid)

    google到数学里定义的群(group): G为非空集合,如果在G上定义的二元运算 *,满足 (1)封闭性(Closure):对于任意a,b∈G,有a*b∈G (2)结合律(Associativit ...

  6. 01背包-第k优解

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  7. lhgDialog使用--loading提示(不自动关闭)

    使用lhgDialog时,发现有一个$.dialog.tips()方法可以实现loading样式的提示,但是存在默认关闭时间.方法如下图所示, 为了实现不自动关闭的方法,查看了相应的源码后,实现不关闭 ...

  8. Timestamp 转 date

    Timestamp startTime = new Timestamp(new Date().getTime());

  9. linux学习之高并发服务器篇(二)

    高并发服务器 1.线程池并发服务器 两种模型: 预先创建阻塞于accept多线程,使用互斥锁上锁保护accept(减少了每次创建线程的开销) 预先创建多线程,由主线程调用accept 线程池 3.多路 ...

  10. python通过sigar收集服务器信息

    http://blog.csdn.net/mirahs/article/details/49681787