Python 中方法参数 * 和 ** 的例子
来源:互联网 发布:股票走势软件 编辑:程序博客网 时间:2024/05/17 09:08
在Python中* 和 ** 有特殊含义,他们与函数有关,在函数被调用时和函数声明时有着不同的行为。此处*号不代表C/C++的指针。
其中 * 表示的是元祖或是列表,而 ** 则表示字典
以下为 ** 的例子:
01
#--------------------第一种方式----------------------#
02
import
httplib
03
def
check_web_server(host,port,path):
04
h
=
httplib.HTTPConnection(host,port)
05
h.request(
'GET'
,path)
06
resp
=
h.getresponse()
07
print
'HTTP Response'
08
print
' status ='
,resp.status
09
print
' reason ='
,resp.reason
10
print
'HTTP Headers:'
11
for
hdr
in
resp.getheaders():
12
print
' %s : %s'
%
hdr
13
14
15
if
__name__
=
=
'__main__'
:
16
http_info
=
{
'host'
:
'www.baidu.com'
,
'port'
:
'80'
,
'path'
:
'/'
}
17
check_web_server(
*
*
http_info)
01
#--------------------第二种方式----------------------#
02
03
04
def
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
h
=
httplib.HTTPConnection(args[
'host'
],args[
'port'
])
17
h.request(
'GET'
,args[
'path'
])
18
resp
=
h.getresponse()
19
print
'HTTP Response'
20
print
' status ='
,resp.status
21
print
' reason ='
,resp.reason
22
print
'HTTP Headers:'
23
for
hdr
in
resp.getheaders():
24
print
' %s : %s'
%
hdr
25
26
27
if
__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)
- Python 中方法参数 * 和 ** 的例子
- HTML传递参数的方法和例子
- python中消除冗余参数的方法
- Python类中方法的self参数
- Python中参数和C/C++中参数的区别
- Python中使用item()方法遍历字典的例子
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- Python中函数的参数定义和可变参数
- C#文件合并
- linux学习心得(二)
- Linux Learning Notes in learning Linux 鸟哥的私房菜 Chapter5
- 生成模型与判别模型
- 一个整数,它加上100后是一个完全平方数,加上168又是一个完全平方数,请问该数是多少?
- Python 中方法参数 * 和 ** 的例子
- VirtualBox虚拟机网络设置
- 第六周项目二(2)
- 【转】未能加载文件或程序集"System.Web.Extensions, Version=1.0.61025.0, Culture=neutral...
- 关于老罗锤子ROM的一些感想
- linux学习心得(三)
- RMAN not always use incremental backup if it has archivelog backup too
- C++ 随机函数----谈rand() 和 srand() 体会
- C语言中volatile关键字的作用