Python 官方文档:入门教程 => 点击学习
>>> help(apply) Help on built-in function apply in module __builtin__: apply(...) apply(object[, args[, kw
>>> 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!
--结束END--
本文标题: python apply()函数
本文链接: https://www.lsjlt.com/news/190227.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0