zoukankan      html  css  js  c++  java
  • 【转载】 Python 方法参数 * 和 **

    Python的函数定义中有两种特殊的情况,即出现*,**的形式。
    如:def myfun1(username, *keys)或def myfun2(username, **keys)等。

    他们与函数有关,在函数被调用时和函数声明时有着不同的行为。此处*号不代表C/C++的指针。

    其中 * 表示的是元祖或是列表,而 ** 则表示字典

    第一种方式:

     1 import httplib
     2 def check_web_server(host,port,path):
     3  h = httplib.HTTPConnection(host,port)
     4  h.request('GET',path)
     5  resp = h.getresponse()
     6  print 'HTTP Response'
     7  print '        status =',resp.status
     8  print '        reason =',resp.reason
     9  print 'HTTP Headers:'
    10  for hdr in resp.getheaders():
    11  print '        %s : %s' % hdr
    12  
    13  
    14 if __name__ == '__main__':
    15  http_info = {'host':'www.baidu.com','port':'80','path':'/'}
    16  check_web_server(**http_info)

    第二种方式:

     1 def check_web_server(**http_info):
     2  args_key = {'host','port','path'}
     3  args = {}
     4  #此处进行参数的遍历
     5  #在函数声明的时候使用这种方式有个不好的地方就是 不能进行 参数默认值
     6  for key in args_key:
     7  if key in http_info:
     8  args[key] = http_info[key]
     9  else:
    10  args[key] = ''
    11  
    12  
    13  h = httplib.HTTPConnection(args['host'],args['port'])
    14  h.request('GET',args['path'])
    15  resp = h.getresponse()
    16  print 'HTTP Response'
    17  print '        status =',resp.status
    18  print '        reason =',resp.reason
    19  print 'HTTP Headers:'
    20  for hdr in resp.getheaders():
    21  print '        %s : %s' % hdr
    22  
    23  
    24 if __name__ == '__main__':
    25  check_web_server(host= 'www.baidu.com' ,port = '80',path = '/')
    26  http_info = {'host':'www.baidu.com','port':'80','path':'/'}
    27  check_web_server(**http_info)

    转载来自:http://my.oschina.net/u/1024349/blog/120298

  • 相关阅读:
    linux下分析Java程序内存汇总
    Linux使用jstat命令查看jvm的GC情况
    linux的top命令参数详解
    项目中常用的19条MySQL优化
    MySQL:(一)
    MySQL:(二)
    微信公众号开发 (3) 菜单处理
    MyBatis项目实战 快速将MySQL转换成Oracle语句
    Spring Boot (5) 整合 RabbitMQ
    系统 (一) Windows10安装Ubuntu子系统
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4354148.html
Copyright © 2011-2022 走看看