美文网首页python基础
__call__直接对实例进行调用

__call__直接对实例进行调用

作者: 徐子鑑 | 来源:发表于2017-09-22 20:09 被阅读4次
    class Student(object):
        def __init__(self, name):
            self.name = name
    
        def __call__(self):
            print('My name is %s.' % self.name)
    
    
    >>> s = Student('Michael')
    >>> s() # self参数不要传入
    My name is Michael.
    

    相关文章

      网友评论

        本文标题:__call__直接对实例进行调用

        本文链接:https://www.haomeiwen.com/subject/voafextx.html