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. apache限制同一ip大并发访问及下载

    内容来自以下网站整理 http://www.jb51.net/article/58060.htmhttp://www.2cto.com/os/201202/120665.htmlhttp://blog ...

  2. MVC NPOI Linq导出Excel通用类

    之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...

  3. 把cmd信息中的正常和异常输出分别输出到不同txt文件中

    场景一: 1.大量滚动信息容纳不下,在小黑屏中被冲刷掉. 2.希望把正常输出和异常输出分别输出到不同地方. 相关命令 一共有4个输出到文件的命令,现以jar命令打war包举例说明: 命令 说明 举例  ...

  4. Expected one result (or null) to be returned by selectOne(), but found 2

    这个问题在于你查询sql返回结果是多个值.一个集合,但是你在service的实现层的dao都调用了.get方法.而是应该使用.getlist方法等.

  5. Appium移动自动化测试之Java篇

    1.环境准备:创建模拟器请参考:http://www.cnblogs.com/mrjade/p/5803131.html 2.新建一个java project,[File]-->[New]--& ...

  6. vs2015 现用插件

    1.abphelper abp app自动生成接口和input output 2.ankhsvn svn插件 3.c#outline2015 大括号折叠 4.codemaid 格式化 5.go to ...

  7. jQuery Mobile 中创建按钮

    在 jQuery Mobile 中创建按钮 jQuery Mobile 中的按钮可通过三种方法创建: 使用 <button> 元素 使用 <input> 元素 使用 data- ...

  8. [工具.tfs]可视化的TFS命令工具——Team Foundation Sidekicks

    工具介绍:http://www.attrice.info/cm/tfs/index.htm Team Foundation Sidekicks is a suite of tools for Micr ...

  9. brew 任何命令 都 报 synatx error

    brew 忽然不能用了,任何命令都报 syntax error near unexpected token `<<<' 解决方案 cd $(brew --prefix) git fe ...

  10. js+html+jquery 个人笔记

    js+html+jquery 笔记 1.获取HTML对象 var obj = document.getElementById(elementId) 对象的值: obj.value() 2.获取jQue ...