发布时间:2019-09-16 07:17:55编辑:auto阅读(5468)
>>> 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报文
50517
49826
40420
37430
31853
28720
27651
22429
22428
20742
513°
1130°
918°
854°
1101°
975°
1590°
2965°
2654°
2026°