[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 function which tries to check if an object exists or not. It has several parameters and some of them have default value. When using this function, the programmer intended to give some values to some certain parameters but keep the rest for default values. However I astonishedly found that the passed parameters are not those as we intended to pass. For example, we have a function defined like this:
def printDefaultParas(para_1, para_2 = False, para_3 = False):
print("para_1=%s, para_2=%s, para_3=%s"%(para_1, para_2, para_3))
Then, we have some other functions called it, for example one of them could be:
def pitfallHappens():
para_1 = "This is Para 1"
para_3 = True
printDefaultParas(para_1, para_3)
What we expected (or we intended) to have is, the function prints out:
para_1=This is Para 1, para_2=False, para_3=True
But what we actually got will be:
para_1=This is Para 1, para_2=True, para_3=False
Why?
In fact, it is because, in the function pitfallHappens(), when we call function printDefaultParas(), these para_1, and para_3 we put in the parameter list, do not stand for the parameters at all. They are just variables happened to be the same name as the parameter names! As a result, Python will put this values accordingly in the sequence of the parameter list, and leave the rest as their default values (if possible). In this case, it gives the variable para_1's value ("This is Para 1") to parameter para_1, and variable para_3's value (True) to parameter para_2, and leave parameter para_3 as its default value (False).
Now we are clear, and function pitfallHappens() could be corrected as:
def pitfallCorrected():
para_1 = "This is Para 1"
para_3 = True
printDefaultParas(para_1 = para_1, para_3 = para_3)
Then pitfallCorrected() will get expected result.
[Python] Pitfalls: About Default Parameter Values in Functions的更多相关文章
- 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 ...
- 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values
对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...
- python's default parameter
[python's default parameter] 对于值类型(int.double)的default函数参数,函数不会保存对默认类型的修改.对于mutable objectd类型的默认参数,会 ...
- 除去Scala的糖衣(13) -- Default Parameter Value
欢迎关注我的新博客地址:http://cuipengfei.me/ 好久没有写博客了,上一次更新竟然是一月份. 说工作忙都是借口,咋有空看美剧呢. 这半年荒废掉博客说到底就是懒,惯性的懒惰.写博客这事 ...
- JavaScript函数的默认参数(default parameter)
JavaScript函数的默认参数(default parameter) js函数参数的默认值都是undefined, ES5里,不支持直接在形参里写默认值.所以,要设置默认值,就要检测参数是否为un ...
- How to: Initialize Business Objects with Default Property Values in XPO 如何:在 XPO 中用默认属性值初始化业务对象
When designing business classes, a common task is to ensure that a newly created business object is ...
- How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象
When designing business classes, a common task is to ensure that a newly created business object is ...
- python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐
## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...
- [Python] Pitfalls: Be Careful with os.chdir
One thing you need to keep in mind is that when using os.chdir to change the working directory of cu ...
随机推荐
- java 基本语法
标识符 标识符就是给变量.类或方法起的名字. 标识符由26个英文字符大小写(a~zA~Z).数字(0~9).下划线(_)和美元符号($)组成 不能以数字开头,不能是关键字 严格区分大小写 标识符的可以 ...
- java io流之字符流
字符流 在程序中一个字符等于两个字节,那么java提供了Reader.Writer两个专门操作字符流的类. 字符输出流:Writer Writer本身是一个字符流的输出类,此类的定义如下: publi ...
- MSSQL附加数据库5120错误(拒绝访问)处理方法
一. 右键需要附加的数据库文件,弹出属性对话框,选择安全标签页. 找到Authenticated Users用户名. 如未找到,进行Authenticated Users用户名的添加. 二. 添加Au ...
- 第三章 centos安装git
yum install git 安装后的默认存放地点/usr/bin/git
- WebService基本概念及原理
一.Web Service基本概念 WebService是一种跨编程语言和跨操作系统平台的远程调用技术.Web Service也叫XML Web Service WebService是一种可以接收从I ...
- 读取XML 发送网页版邮件
DataSet ds = new DataSet(); ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "XML\\Mail.xml&q ...
- 记录一次自己对nginx+fastcgi(fpm)+mysql压力测试结果
nginx + fastcgi(fpm) 压力测试: CentOS release 5.9 16核12G内存 静态页面: 并发1000,压测200秒,测试结果: 系统最大负载5.47 成功响应: 25 ...
- java8中CAS的增强
注:ifeve.com的同名文章为本人所发,此文在其基础做了些调整.转载请注明出处! 一.java8中CAS的增强 前些天,我偶然地将之前写的用来测试AtomicInteger和synchronize ...
- 魔性の分块 | | jzoj1243 | | 线段树の暴力
题目的打开方式是酱紫的 然而作为一只蒻蒟根本不会线段树该怎么办呢? sro MZX orz 是这样说的:用分块啊! 分块 根据紫萱学姐的教程,分块的打开姿势是这样的: 我们要对一个数组进行整体操作 ...
- myeclipse性能优化
1. 取消启动项.这个设置立竿见影.Window->Preferences->General->Startup and Shutdown, Plug-ins activated on ...