__call__

__call__ 函數,將類實例當作函數使用,弱化了函數和對象的區別。函數

class Person(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

    def __call__(self, friend):
        print('I am %s' % self.name)
        print('My friend is %s' % friend)

p = Person('Bob', 'male')
p('Tim')
本站公眾號
   歡迎關注本站公眾號,獲取更多信息