Python中的call方法
在 Python 中提供了__call__ 方法,允许创建可调用的对象(实例)。如果类中实现了 __call__ 方法,则可以像使用函数一样使用类。#!/usr/bin/env python# -*- coding: utf-8 -*-class Run(object):def __init__(self):passdef __call__(self, data):print('data=%s'
·
在 Python 中提供了__call__ 方法,允许创建可调用的对象(实例)。如果类中实现了 __call__ 方法,则可以像使用函数一样使用类。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Run(object):
def __init__(self):
pass
def __call__(self, data):
print('data=%s' % data)
return data
if __name__ == "__main__":
r = Run()
print(r("python探路者"))
输出:
data=python探路者
python探路者
更多推荐



所有评论(0)