任何类,只需要定义一个__call__()方法,就可以直接对实例进行调用:
class Student1(object):
def __init__(self, name):
self.name = name
def __call__(self):
print('My name is %s.' %self.name)
s1 = Student1('hhh')
print(s1())
输出
My name is hhh.
任何类,只需要定义一个__call__()方法,就可以直接对实例进行调用:
class Student1(object):
def __init__(self, name):
self.name = name
def __call__(self):
print('My name is %s.' %self.name)
s1 = Student1('hhh')
print(s1())
输出
My name is hhh.
本文标题:__call__
本文链接:https://www.haomeiwen.com/subject/fvkxqqtx.html
网友评论