python函数参数中,有时会见到单星号和双星号,他们的作用是:
单星号:传入任意长度的元组
双星号:传入任意长度的字典
例如:
#!/usr/bin/env python
# encoding: utf-8
def one_star(*args):
print '%s:%s'%(type(args),args)
def two_stars(**args):
print '%s:%s'%(type(args),args)
one_star()
one_star('abc',123)
two_stars()
two_stars(a=1,b=2)
输出的结果:
<type 'tuple'>:()
<type 'tuple'>:('abc', 123)
<type 'dict'>:{}
<type 'dict'>:{'a': 1, 'b': 2}
在函数需要可变参数时,这种方法特别有用。