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. NPOI MVC 模型导出Excel通用类

    通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...

  2. 基于jquery封装的颜色下拉选择框

    应同事要求,花了半个小时,写了一个简单的选择颜色的下拉框控件,可以控制输入框指示结果颜色 也贴出来,说不定哪天有用 if (typeof jQuery === 'undefined') { throw ...

  3. SWT组件添加事件的四种方式

    在我们CS日常开发过程中会经常去为组件添加事件,我们常用的为AWT与SWT.SWT的事件模型是和标准的AWT基本一样的.下面将按照事件的四种写法来实现它. 一.匿名内部类的写法 new MouseAd ...

  4. JVM内存区域介绍

    学习JVM第一个要了解的就是JVM的内存区域. Java虚拟机在运行时会从操作系统内存中划分一部分出来作为JVM内存,而JVM内存又划分为以下几个区域: 大体上可以分为两种: 线程共享数据区 该类型的 ...

  5. dp与px转换

    名词 解释 Px (Pixel像素) 不同设备显示效果相同.这里的“相同”是指像素数不会变,比如指定UI长度是100px,那不管分辨率是多少UI长度都是100px.也正是因为如此才造成了UI在小分辨率 ...

  6. js 让浏览器全屏模式的方法launchFullscreen

    浏览器全屏模式的启动函数requestFullscreen仍然需要附带各浏览器的js方言前缀 // 判断各种浏览器,找到正确的方法 function launchFullscreen(element) ...

  7. Light OJ 1027 - A Dangerous Maze (数学-期望)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题目大意: 一个迷宫, 有n个门,选择一个门花费为|ai|, 如果选择的 ...

  8. ios label上显示特殊字符 % "

    今天在label上显示一个拼接的百分比 label.text = [NSString stringWithFormater:@"%d%",i]; 结果后面的%就是报错,然后查半天也 ...

  9. 合并两个排好序的链表(c++)

    #include<iostream> struct node{ int payload; node* next; node(int payload){this->payload=pa ...

  10. [[UIScreen mainScreen] bounds] 返回的屏幕尺寸不对

    在使用cocos2d-iphone 2.0生成项目的时候,用5s测试时全屏中上下一直有黑条,发现[[UIScreen mainScreen] bounds]返回的屏幕尺寸不是320*568的,而是32 ...