[Python] Problem with Default Arguments
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的更多相关文章
- 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") ...
- Python TypeError: not enough arguments for format string
今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...
- 类模板成员函数默认值问题: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编译器会报 ...
- Templates and Default Arguments
Default parameters for templates in C++: Like function default arguments, templates can also have de ...
- Default arguments and virtual function
Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...
- [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 ...
- Python TypeError: not all arguments converted during string formatting ——元组tuple(a)和(a,)的区别
今天写程序,想输出一个array的shape,原程序为: print('shape of testUImatrix:%s\nStart to make testUImatrix...'%(testui ...
- [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 ...
- [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 ...
随机推荐
- numeric and int in sql server
类型映射 https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings C#关 ...
- centos7 更改网络后 无法重启network (解决办法)
今天由于用nat与本地局域网冲突,可能和之前ENSP搭建时虚拟机冲突造成 然后修改了VMnet8的ip,网关,DNS 同时更改虚拟网络编译器与VMnet8保持相同 进入虚拟机,更改/etc/sysco ...
- IHttpHandler的学习(0-2)
这个IHttpHandler,要想到asp.net生命周期 ,想到哪个从你发起请求开始,这个请求通过HttpModule------>IHttpHandler的: 执行HttpModule的一系 ...
- T_SQL 日期函数
日期函数基数表达式的日期和时间或者是从时间间隔中返回值. GETDATE(),返回当前系统的日期和时间.例: SELECT GETDATE(); 结果为:2010-05-18 15:53:08.92 ...
- BZOJ 3637: Query on a tree VI LCT_维护子树信息_点权转边权_好题
非常喜欢这道题. 点权转边权,这样每次在切断一个点的所有儿子的时候只断掉一条边即可. Code: #include <cstring> #include <cstdio> #i ...
- JS获取当前时间(YYYY-MM-DD ),element显示默认当前时间,显示默认昨天,显示默认上个月
原文链接:点我 进来的随便看看,或许有帮助 vue+element-ui datepicker 设置默认日期用的框架是vue+element-ui ,以下是时间控件 <el-form-ite ...
- man帮助
man命令是Linux下的帮助指令,通过man指令可以查看Linux中的指令帮助.配置文件帮助和编程帮助等信息.
- CodeForces 363B Fence
Fence Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...
- 【转】 java RSA加密解密实现
[转] java RSA加密解密实现 该工具类中用到了BASE64,需要借助第三方类库:javabase64-1.3.1.jar 下载地址:http://download.csdn.net/detai ...
- WIN10远程桌面连接--“出现身份验证错误。要求的函数不支持”
最近WIN10升级补丁后发现远程桌面无法连接了,报“出现身份验证错误.要求的函数不支持”的错误: 解决办法: 第一种,配置本地自己的电脑,开始菜单->搜索gpedit.msc并打开 打开配置 ...