Python: 什么是*args和**kwargs

先来看个例子:

def foo(*args, **kwargs): print ‘args = ‘, args print ‘kwargs = ‘, kwargs print ‘—————————————’ if __name__ == ‘__main__’: foo(1,2,3,4) foo(a=1,b=2,c=3) foo(1,2,3,4, a=1,b=2,c=3) foo(’a', 1, None, a=1, b=’2′, c=3)输出结果如下:

args =  (1, 2, 3, 4)
kwargs =  {}
—————————————
args =  ()
kwargs =  {’a': 1, ‘c’: 3, ‘b’: 2}
—————————————
args =  (1, 2, 3, 4)
kwargs =  {’a': 1, ‘c’: 3, ‘b’: 2}
—————————————
args =  (’a', 1, None)
kwargs =  {’a': 1, ‘c’: 3, ‘b’: ‘2′}
—————————————


可以看到,这两个是python中的可变参数。*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前,像foo(a=1, b=’2′, c=3, a’, 1, None, )这样调用的话,会提示语法错误“SyntaxError: non-keyword arg after keyword arg”。


 


呵呵,知道*args和**kwargs是什么了吧。还有一个很漂亮的用法,就是创建字典:

def kw_dict(**kwargs): return kwargs print kw_dict(a=1,b=2,c=3) == {’a':1, ‘b’:2, ‘c’:3}

其实python中就带有dict类,使用dict(a=1,b=2,c=3)即可创建一个字典了。


 


“人生苦短,我用python。”

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Related posts:

  1. Python:什么是*args和**kwargs

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu