[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 ...
随机推荐
- redis配置文件redis.conf说明
redis.conf 配置项说明如下:1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no2. 当Redis以守护进程方式运行时, ...
- iOS中的上传、下载流程心得
访问相册 1. 判断资源库是否有效 2. 创建imagePickerController 设置代理 弹出视图控制器 3. 实现协议方法 > iOS10 访问系统相册需要在info.plis ...
- css总结
1. css中的role属性 html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组 ...
- Python SQLAlchemy --1
本文為 Python SQLAlchemy ORM 一系列教學文: SQLAlchemy 大概是目前 Python 最完整的資料庫操作的套件了,不過最令人垢病的是它的文件真的很難閱讀,如果不搭配個實例 ...
- De4Dot+Reflector 支持多种反混淆
官网: http://www.de4dot.com/ 源码:https://github.com/brianhama/de4dot 使用方法 通过CMD命令方式进入: F:\2\de4dot-v3-1 ...
- 发送短信MFMessageComposeViewController
if([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController * controller = [[M ...
- [z]START WITH CONNECT BY PRIOR子句实现递归查询
[z]http://jingyan.baidu.com/article/5d368d1e182bb93f60c05784.html START WITH CONNECT BY PRIOR这个语法主要用 ...
- 百度UEditor基本使用
1 首先奉上链接其http://ueditor.baidu.com/website/index.html 更多更详细内容在其官方api上,本文只是一个归类总结性文章. 2 下载链接http://ued ...
- shell常用命令
wget wget -P /root url -O rename 实现文件下载指定目录和重命名
- sql月份销售统计
1.SELECT MONTH(SellTime) as selltime,SUM(TotalPrice) as total FROM Sell WHERE YEAR(SellTime)=CON ...