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. 2015-8-29阿里校园招聘研发project师笔试题

    前言:原题来自于网络:http://www.cnblogs.com/nausicaa/p/3946694.html.本人依据自己理解对题目进行解答.因为水平有限.题目有不会做.做错的地方.欢迎大家留言 ...

  2. oracle 存储过程定义及调试,并终于被C# 调用 代码

    C# 调用存储过程 參考了非常多文章,写了例如以下文字,算是分享吧 目的:更改积分,并作一定校验 一.一般的调试方法: 方法一:带返回out參数,必须定义变量 myresult DECLARE myr ...

  3. javascript面向对象编程,带你认识封装、继承和多态

    原文链接:点我 周末的时候深入的了解了下javascript的面向对象编程思想,收获颇丰,感觉对面向对象编程有了那么一丢丢的了解了~很开森 什么是面向对象编程 先上一张图,可以对面向对象有一个大致的了 ...

  4. Linux Shell脚本编程-函数

    函数介绍  定义:把一段独立功能的的代码当做一个整体,并为之一个名字,命名的代码段,此即为函数:  功能:函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程.  注意: ...

  5. shell清除日志小脚本

    #!/bin/bash #清除日志脚本 LOG_DIR=/var/log ROOT_UID=0 #用户id为0的 ,即为root if [ "$UID" -ne "$RO ...

  6. caioj 1079 动态规划入门(非常规DP3:钓鱼)(动规中的坑)

    这道题写了我好久, 交上去90分,就是死活AC不了 后来发现我写的程序有根本性的错误,90分只是数据弱 #include<cstdio> #include<algorithm> ...

  7. 《AndroidStudio每日一贴》5. 怎样高速查看某个方法/注解的定义?

    操作方法: 使用快捷键 option + space 或 command + y 举个样例: 如以下的样例,我在输入@O的时候会出现代码补全列表,这个时候我想查看列表中项目的定义能够使用快捷键 opt ...

  8. 多client并发登录

    //LoginClient.java package mySocket; import java.io.BufferedReader; import java.io.InputStreamReader ...

  9. Python带括号的计算器

    带括号的计算器也是第一个自我感觉完成最好的 毕竟真的弄了一个多星期 虽然前期这路真的很难走  我会努力加油  将Python学好学踏实 参考了两位博主的文章 http://www.cnblogs.co ...

  10. string类自定义字符串替换函数replace

    #include <iostream> #include <string> using namespace std; /* *  函数功能:将string字符串中的某些字符替换 ...