Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26277605
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-02-06 11:08:35

示例代码如下:
def foo(*args,**s):
    print 'args=',args
    print 'kwargs',s
    print '--------------------------'
if __name__ == '__main__':
    foo(1,2,3,4)
    foo(a=1,b=2,c=3)
    foo(1,2,3,4,a=1,b=2,c=3)
    foo('a',1,None,a=1,b='2',c=3)
输出结果如下:
args= (1, 2, 3, 4)
kwargs {}
--------------------------
args= ()
kwargs {'a': 1, 'c': 3, 'b': 2}
--------------------------
args= (1, 2, 3, 4)
kwargs {'a': 1, 'c': 3, 'b': 2}
--------------------------
args= ('a', 1, None)
kwargs {'a': 1, 'c': 3, 'b': '2'}
--------------------------
结论:
* 打头的 参数表示的是 元组()  无名参数  foo(1,2,3,4) 而且是无名可变参数的调用
** 打头的  参数表示的是 字典{} 有名可变参数 foo(a=1,b=2,c=3)

经常会看到有一些类的构造函数的写法就有这样的。
class PersistentDB:
    version = __version__
    def __init__(self, creator,maxusage=None, setsession=None, failures=None,
            closeable=False, threadlocal=None, *args, **kwargs):
看到没有 *args, **kwargs

args, kwargs: the parameters that shall be passed to the creator
function or the connection constructor of the DB-API 2 module

import threading,time,datetime
import MySQLdb
import DBUtils.PersistentDB
persist = DBUtils.PersistentDB.PersistentDB(MySQLdb,100,host='localhost',user='root',passwd='321',db='test')
conn = persist.connection()

指定其构造函数参数



阅读(609) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-06-03 11:02:19

结论: * 打头的 参数表示的是 元组() 无名参数 foo(1,2,3,4) 而且是无名可变参数的调用 ** 打头的 参数表示的是 字典{} 有名可变参数 foo(a=1,b=2,c=3)