在Python中* 和 ** 有特殊含義,他們與函數有關,在函數被調用時和函數聲明時有着不一樣的行爲。此處*號不表明C/C++的指針。 python
其中 * 表示的是元祖或是列表,而 ** 則表示字典 web
如下爲 ** 的例子: 函數
#--------------------第一種方式----------------------# import httplib def check_web_server(host,port,path): h = httplib.HTTPConnection(host,port) h.request('GET',path) resp = h.getresponse() print 'HTTP Response' print ' status =',resp.status print ' reason =',resp.reason print 'HTTP Headers:' for hdr in resp.getheaders(): print ' %s : %s' % hdr if __name__ == '__main__': http_info = {'host':'www.baidu.com','port':'80','path':'/'} check_web_server(**http_info)另外一種方式:
#--------------------第二種方式----------------------# def check_web_server(**http_info): args_key = {'host','port','path'} args = {} #此處進行參數的遍歷 #在函數聲明的時候使用這種方式有個很差的地方就是 不能進行 參數默認值 for key in args_key: if key in http_info: args[key] = http_info[key] else: args[key] = '' h = httplib.HTTPConnection(args['host'],args['port']) h.request('GET',args['path']) resp = h.getresponse() print 'HTTP Response' print ' status =',resp.status print ' reason =',resp.reason print 'HTTP Headers:' for hdr in resp.getheaders(): print ' %s : %s' % hdr if __name__ == '__main__': check_web_server(host= 'www.baidu.com' ,port = '80',path = '/') http_info = {'host':'www.baidu.com','port':'80','path':'/'} check_web_server(**http_info)