python函数参数总结

python中函数参数有:默认参数、关键字参数、非关键字可变长参数(元组)、关键字可变长参数(字典)

默认参数:在函数声明时,指定形参的默认值,调用时可不传入改参数(使用默认值)
def foo(x): ##默认参数
  print 'x is %s' % x
关键字参数: y默认为20
def foo( x,y=20): ##关键字参数
  print 'x is %s' % x
  print 'y is %s' % y
非关键字可变长参数(元组):*z接收一个元组
def foo(x,y=20,*z): ##默认参数
  print 'x is %s' % x
  print 'y is %s' % y
  for myz in z:
    print 'z: ', myz
关键字可变长参数(字典):**w接收的是一个字典
def foo(x,y=20,*z,**w):  ##默认参数
  for wArg in w.keys():
        print 'wArg %s: %s' % (wArg, str(w[wArg]))






原文地址:https://www.cnblogs.com/seablog/p/6985417.html