首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Python 中步骤参数 * 和 * 的例子

2013-04-09 
Python 中方法参数 * 和 ** 的例子在Python中* 和 ** 有特殊含义,他们与函数有关,在函数被调用时和函数声

Python 中方法参数 * 和 ** 的例子

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

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

以下为 ** 的例子:

01#--------------------第一种方式----------------------#02import httplib03def check_web_server(host,port,path):04 = httplib.HTTPConnection(host,port)05 h.request('GET',path)06 resp = h.getresponse()07 print 'HTTP Response'08 print '        status =',resp.status09 print '        reason =',resp.reason10 print 'HTTP Headers:'11 for hdr in resp.getheaders():12 print '        %s : %s' % hdr13 14 15if __name__ == '__main__':16 http_info = {'host':'www.baidu.com','port':'80','path':'/'}17 check_web_server(**http_info)另一种方式:01#--------------------第二种方式----------------------#02 03 04def check_web_server(**http_info):05 args_key = {'host','port','path'}06 args = {}07 #此处进行参数的遍历08 #在函数声明的时候使用这种方式有个不好的地方就是 不能进行 参数默认值09 for key in args_key:10 if key in http_info:11 args[key] = http_info[key]12 else:13 args[key] = ''14 15 16 = httplib.HTTPConnection(args['host'],args['port'])17 h.request('GET',args['path'])18 resp = h.getresponse()19 print 'HTTP Response'20 print '        status =',resp.status21 print '        reason =',resp.reason22 print 'HTTP Headers:'23 for hdr in resp.getheaders():24 print '        %s : %s' % hdr25 26 27if __name__ == '__main__':28 check_web_server(host= 'www.baidu.com' ,port = '80',path = '/')29 http_info = {'host':'www.baidu.com','port':'80','path':'/'}30 check_web_server(**http_info)

热点排行