1 apply 函数
def printInfo(id,name,address):
print 'id is ',id,' and name is ',name,' and address is ',address
1.1 调用printInfo函数
printInfo(1,'flankwang','HeNan KaiFeng')
1.2 调用printInfo函数
apply(printInfo,(1,'flankwang','HeNan KaiFeng'))
1.1 和1.2效果一样
2 __call__ 函数
class CallDemo(object):
def __init__(self):
print 'this is __init__ method'
def __call__(self):
print 'this is __call__ method'
调用:
CallDemo()()
结果:
this is __init__ method
this is __call__ method
本文介绍了Python中apply函数的使用方法,并通过实例演示了如何利用apply调用自定义函数。此外,还探讨了__call__魔法方法的实现原理及应用场景,展示了如何通过将类实例作为函数进行调用来实现特定功能。
265

被折叠的 条评论
为什么被折叠?



