发布时间:2019-09-16 07:17:55编辑:auto阅读(5071)
>>> help(apply)
Help on built-in function apply in module __builtin__:
apply(...)
apply(object[, args[, kwargs]]) -> value
Call a callable object with positional arguments taken from the tuple args,
and keyword arguments taken from the optional dictionary kwargs.
Note that classes are callable, as are instances with a __call__() method.
<span style="color:#ff0000;">Deprecated since release 2.3. Instead, use the extended call syntax:
function(*args, **keywords).</span>
>>>
1》执行不带参数的函数
def say():
print 'hello python!'
say()
apply(say)
结果:def say(a):
print a
say('hello python!')
apply(say,("hello python!",))
def say_again(a,b):
print a,b
say_again('hello','python!')
apply(say_again,('hello','python!'))
结果:def say(a=1,b=2):
print a,b
def haha(**kw):
print kw
print type(kw)
say()
say(kw)#将kw传给a, b取默认值
apply(say,(),kw)
haha(a='hello',b='python!')
结果:hello python!
又如:
def say(x,y,a=1,b=2):
print x,y,a,b
def haha(*args,**kw):
print args,type(args)
print kw,type(kw)
apply(say,args,kw)
haha(1,2,a='hello',b='python!')
结果:
(1, 2) <type 'tuple'>
{'a': 'hello', 'b': 'python!'} <type 'dict'>
1 2 hello python!
上一篇: python3 GUI
下一篇: python 生成拼接xml报文
47842
46390
37284
34733
29313
25973
24916
19951
19545
18030
5792°
6413°
5927°
5961°
7064°
5911°
5944°
6438°
6404°
7778°