99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋果6袋
6
麥子學(xué)院

Python學(xué)習(xí)之*args和**kwargs詳解

發(fā)布時(shí)間:2017-09-07 19:37  回復(fù):0  查看:2786   最后回復(fù):2017-09-07 19:37  
本文和大家分享的主要是python *args **kwargs相關(guān)內(nèi)容,一起來看看吧,希望對(duì)大家 學(xué)習(xí)python 有所幫助。
   0X00 *args是什么
  我們知道Python3 中的 print 從一個(gè)關(guān)鍵字變成了一個(gè)函數(shù),那么調(diào)用的時(shí)候我們可以這樣調(diào)用這個(gè)函數(shù),可以隨便接受幾個(gè)參數(shù)。
>>> print(1)
1
>>> print(1, 2, 3)
1 2 3
>>> print(1, "hello", 6.66)
1 hello 6.66
  那么如果我們想自己實(shí)現(xiàn)類似這樣變態(tài) 的函數(shù)該怎么實(shí)現(xiàn)呢?這就需要用到 *args 了,可以將一個(gè)非鍵值對(duì)的可變數(shù)量的參數(shù)列表傳給一個(gè)函數(shù)(換個(gè)書佛?。嚎梢詡?/span> n 個(gè)參數(shù)給函數(shù),而且 n 不是固定的),舉個(gè)例子就容易理解多了。
   def  say_something(*args):
   for i  in args:
   print i
   print '--------'
  say_something(1)
  say_something(1, 2, 3)
  say_something('hello')
  say_something('hello', 'world')
  運(yùn)行這個(gè)例子的輸出就是這樣的
  1--------123--------
  hello--------
  hello
  world--------
  還有一個(gè)更棒的例子  來自Gitbook
   def  test_var_args(f_arg, *args):
  print("first normal arg:", f_arg)
   for arg  in args:
  print("another arg through *args:", arg)
  test_var_args('yasoob', 'python', 'eggs', 'test')
  輸出是這樣的
  ('first normal arg:', 'yasoob')
  ('another arg through *args:', 'python')
  ('another arg through *args:', 'eggs')
  ('another arg through *args:', 'test')
  這個(gè)例子完整的說明了  \*args  的用法,我們傳入的第一個(gè)參數(shù)被函數(shù)指定的  f_arg  接收到了,其余的都被  *args  接收到了。
   0X01 **kwargs是什么
  寫代碼的時(shí)候還會(huì)有一種函數(shù)調(diào)用,大概是這個(gè)樣子  json.dumps(dict_data)    json.dumps(dict_data, indent=4)  。當(dāng)然,實(shí)現(xiàn)這種的方式有一個(gè)最簡單的方案就是  def dumps(input_data, indent=0)  。在可選參數(shù)只有一兩個(gè)的時(shí)候這種方式固然是好用的,但是如果像是requests 這種庫中的常用方法,有很多很多個(gè)可選參數(shù)那就該用上這個(gè) **kwargs 了。顧名思義這個(gè)就是  keyworkargs  的意思,也就是說是帶有key 的可變參數(shù)??梢赃@樣定義一個(gè)函數(shù)
   def  foo(**kwargs):
   for key  in kwargs:
   print key
   print kwargs[key]
   print '-----'
  foo(a=1, b=2, c=3, d=4, e=5)
  運(yùn)行出來的結(jié)果可想而知:
  a1-----
  c3-----
  b2-----
  e5-----
  d4-----
   0X02 合在一起怎么用
  值得一提的是如何把這兩個(gè)放在一起用,這里列舉個(gè)例子來演示一下
  #!/usr/bin/env python# coding=utf-8
   def  foo(name, sex, *args, **kwargs):
   print 'name is ', name
   print 'sex is ', sex
   print 'other is ', args
   for key  in kwargs:
   print key, ' is ', kwargs[key]
   def  bar(*args, **kwargs):
   print 'args is ', args
   print 'kwargs is ', kwargs
  foo('shawn', '???', 'hello', 'world', hobby='computer', number=666) print '--------------------------'
  bar('shawn', '???', 'hello', 'world', hobby='computer', number=666)
  輸出結(jié)果是這樣的
  name  is  shawn
  sex  is  ???
  other  is  ('hello', 'world')
  hobby   is  computer number   is  666
  -------------------------- args  is  ('shawn', '???', 'hello', 'world')
  kwargs  is  {'hobby': 'computer', 'number': 666}
  這里有需要注意的一點(diǎn):參數(shù)的名字不一定非要是  *args    **kwargs  ,所以我們定義函數(shù)的時(shí)候不一定是  def foo(*args, **kwargs):  ,也同樣可以定義成  def bar(*hehe, **haha):  ,這里真正標(biāo)識(shí)的是星號(hào)而不是名字。不過建議命名的時(shí)候符合大家的習(xí)慣。
來源:Shawn's Blog

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?