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

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

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

第一种方式:

 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':'','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 = '',path = '/')
http_info = {'host':'www.baidu.com','port':'','path':'/'}
check_web_server(**http_info)

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

【转载】 Python 方法参数 * 和 **的更多相关文章

  1. [转载]Python方法绑定——Unbound/Bound method object的一些梳理

    本篇主要总结Python中绑定方法对象(Bound method object)和未绑定方法对象(Unboud method object)的区别和联系.主要目的是分清楚这两个极容易混淆的概念,顺便将 ...

  2. python方法参数:*和**操作符

    * def test_args_kwargs(arg1, arg2, arg3): print("arg1:", arg1) print("arg2:", ar ...

  3. odoo14 button 事件调用python方法如何传递参数

    1 <field name="user_ids" 2 mode="kanban" 3 nolabel="1" 4 options=&q ...

  4. [转载] Python数据类型知识点全解

    [转载] Python数据类型知识点全解 1.字符串 字符串常用功能 name = 'derek' print(name.capitalize()) #首字母大写 Derek print(name.c ...

  5. Java可变参数 & Python可变参数 & Scala可变参数

    Java 可变参数的特点: (1).只能出现在参数列表的最后: (2)....位于变量类型和变量名之间,前后有无空格都可以: (3).调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体 ...

  6. python函数参数的pack与unpack

    python函数参数的pack与unpack 上周在使用django做开发的时候用到了mixin(关于mixin我还要写一个博客专门讨论一下,现在请参见这里),其中又涉及到了一个关于函数参数打包(pa ...

  7. Python中参数是传值,还是传引用?

    在 C/C++ 中,传值和传引用是函数参数传递的两种方式,在Python中参数是如何传递的?回答这个问题前,不如先来看两段代码. 代码段1: def foo(arg): arg = 2 print(a ...

  8. 关于laravel5.5控制器方法参数依赖注入原理深度解析及问题修复

    在laravel5.5中,可以根据控制器方法的参数类型,自动注入一个实例化对象,极大提升了编程的效率,但是相比较与Java的SpringMVC框架,功能还是有所欠缺,使用起来还是不太方便,主要体现在方 ...

  9. Python函数参数的五种类型

    之前项目需求,需要通过反射获取函数的参数,python中可以通过函数签名(signature)来实现. 首先需要了解函数参数的类型,Python的参数类型一共有5种:POSITIONAL_OR_KEY ...

随机推荐

  1. Linux之Shell的算术运算

    在Bash的算术运算中有以下几种方法:名称                语法                    范例算术扩展            $((算术式))              r ...

  2. 剑指Offer 把字符串转换成整数

    题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串,包括数字字母符号,可以为空 输出描述: 如果是合法 ...

  3. javascript 常用技巧

    1. 将彻底屏蔽鼠标右键 oncontextmenu=”window.event.returnValue=false” < table border oncontextmenu=return(f ...

  4. 无密码执行sudo指令

    参考:http://askubuntu.com/questions/334318/sudoers-file-enable-nopasswd-for-user-all-commands 配置: 执行vi ...

  5. __getattr__ 与动态属性

    直接上代码 >>> class Test(object): ... def __getattr__(self,attr_name): ... setattr(self, attr_n ...

  6. [转]Aptana Studio 3配置Python开发环境图文教程

    转载URL:http://www.cr173.com/html/49260_1.html 一.安装Aptana Studio 3 安装完运行时建议将相关默认工作目录设定在英文的某个目录下.避免可能出现 ...

  7. 使用 MongoDB 的_id 查询

    MongoDB 默认在插入数据时,生成一个主键_id,那么怎么使用_id来查询数据? 查询全部 > db.foo.find(){ "_id" : ObjectId(" ...

  8. 【GoLang】golang垃圾回收 & 性能调优

    golang垃圾回收 & 性能调优 参考资料: 如何监控 golang 程序的垃圾回收_Go语言_第七城市 golang的垃圾回收(GC)机制 - 两只羊的博客 - 博客频道 - CSDN.N ...

  9. [转]Java连接各种数据库的方法

    //MySQL:       String Driver="com.mysql.jdbc.Driver";   //驱动程序     String URL="jdbc:m ...

  10. sizeof进行结构体大小的判断

    typedef struct{    int a;    char b;}A_t;typedef struct{    int a;    char b;    char c;}B_t;typedef ...