函数参数可以设置缺省值
warning:The default value is evaluated only once. This makes a difference when the default is a mutable object such as list, dictionary, or instances of most classes.
def f(a, L=[]):
L.append(a)
return L
print f(1), f(2), f(3)
-------------------------
[1] [1, 2] [1, 2, 3]
def f(a, L=None):
if L is None:
L = []
L.append(a)
return L
-----------------------
[1] [2] [3]
*与**
函数定义时:
*args表示接受到的参数会以tuples的形式呈现**kwargs表示接受到的参数会以dictionary的形式呈现
函数调用时:
* : to unpack the arguments out of a list or tuple**: to unpack the arguments out of dictionary
def hello(a, b):
print a, b
hello(*[2015, 'hello'])
----------------------
2015 hello
def hello(a, b='world'):
print a, b
hello(**{'a':'Hello', 'b':'World'})
----------------------
Hello World