__call__

作者: hie | 来源:发表于2018-11-20 16:18 被阅读0次

任何类,只需要定义一个__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