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的更多相关文章

  1. 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 ...

  2. 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values

    对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...

  3. python's default parameter

    [python's default parameter] 对于值类型(int.double)的default函数参数,函数不会保存对默认类型的修改.对于mutable objectd类型的默认参数,会 ...

  4. 除去Scala的糖衣(13) -- Default Parameter Value

    欢迎关注我的新博客地址:http://cuipengfei.me/ 好久没有写博客了,上一次更新竟然是一月份. 说工作忙都是借口,咋有空看美剧呢. 这半年荒废掉博客说到底就是懒,惯性的懒惰.写博客这事 ...

  5. JavaScript函数的默认参数(default parameter)

    JavaScript函数的默认参数(default parameter) js函数参数的默认值都是undefined, ES5里,不支持直接在形参里写默认值.所以,要设置默认值,就要检测参数是否为un ...

  6. 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 ...

  7. 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 ...

  8. python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐

    ## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...

  9. [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 ...

随机推荐

  1. b/s结构的物业管理系统(一)-------登录篇

    最近计划做一个非框架的物业管理系统前端使用bootstrap js jquery 等希望各位指点一下共同学习 ---前端登录页面------ 这个页面的输入框组用的bootstrap的,我设置了几张背 ...

  2. oracle常用命令大全及心得

    学习时整理的 Oracle 1.set linesize 100; 设置长度2.set pagesize 30; 设置每页显示数目3.em a.sql 打开记事本 4.@ a 执行文件a中的代码,可指 ...

  3. pooling的原理与Python实现

    本文首先阐述pooling所对应的操作,然后分析pooling背后蕴含的一些道理,最后给出pooling的Python实现. 一.pooling所对应的操作 首先从整体上对pooling有一个直观的概 ...

  4. Git项目存放位置在导入Eclipse前不能存放在Eclipse Workspace

    这篇帖子的背景: 本人想将一个git项目导入至Eclipse的Workspace中,并且该项目的所有git信息.但是,该git项目在导入之前,就已经存放在Eclipse的Workspace中.在将该g ...

  5. java.sql.SQLException: ORA-00911: invalid character 解决方法

    java.sql.SQLException: ORA-00911: invalid character 控制台抛出这个异常:java.sql.SQLException: ORA-00911: inva ...

  6. Newtonsoft.Json解析Json字符串案例:

    /// <summary> /// 上行jsom格式日志记录 /// </summary> /// <param name="responseJson" ...

  7. 为什么重写equals时必须重写hashCode方法?

    原文地址:http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html 首先我们先来看下String类的源码:可以发现Stri ...

  8. Zookeeper-Zookeeper可以干什么

    在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...

  9. easyui 下拉树改造

    <select id="cc" style="width: 250px"></select> <div id="sp&q ...

  10. 【随笔】js加载

    有时候,当发现js操作一个dom的时候,发现dom没有找到,这是由于html没有加载完就开始操作该dom的缘故,所以需要在html文档加载完后再加载js,于是我们可以这么做: js方法:window. ...