【Python 2.6.6】自定义函数的参数的默认值
测试环境
1 2 3 4 5 |
[root@dt xxf]# python Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> |
以上一篇文章中创建的hello.py为例,完成函数中的参数包含默认值的测试
1 2 3 4 5 |
def hello(name = 'World'): print 'hello ' + name hello() hello('DBA!') |
我们看上面的代码会发现,参数name赋予了一个值“World”,意思是,当hello这个函数在调用时,如果传参数了,那么使用你传的参数,如果没有传参数,那么就使用参数的默认值。我们可以看下面两次调用,第一次调用未传参,第二次调用传了“DBA!”,那么我们执行一下,看看是不是我们描述的效果,如下:
1 2 3 4 |
[root@dt xxf]# python hello.py hello World hello DBA! [root@dt xxf]# |