• 每天进步一点点!

文章分类

推荐网站

常用手册

python中apply函数的使用【原创】

<<返回

2013-11-08 15:46:53

 

Python 允许你实时地创建函数参数列表. 只要把所有的参数放入一个元组中,然后通过内建的 apply函数调用函数.

 

#!/usr/bin/python

def test(a, b) :
    print a, b

apply(test, ('hello', 'world'))
apply(test, (1, 2 + 3)) 

r =  apply(lambda x, y : x + y, (1, 2)) 
print r

apply(test, (), {'a' : 'avalue', 'b' : 'bvalue'})

#apply(test, {'a' : 'avalue', 'b' : 'bvalue'})
#apply(test, {'name' : 'fisher', 'sex' : 'm'})

 

被注释掉的两个不正确,参数必须是元祖,哪怕是空元祖,而且键值必须与函数的参数名称一致。如上例中,字典的键名称必须与test的参数名称一致。

文章评论

  • 暂无评论

发表评论

昵称:

内容:

发表评论