__call__

2018-11-20  本文已影响0人  hie

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

上一篇 下一篇

猜你喜欢

热点阅读