python中__str__, __call__方法

来源:互联网 发布:音乐效果器软件 编辑:程序博客网 时间:2024/06/05 02:00

call在加上()时执行

class Foo:    def __init__(self):        pass    def __call__(self, *args, **kwargs):        print "__call__"obj = Foo()obj() #自动执行此方法__call__

输出 print ‘对象’ 自动调用str方法

class Foo:    def __init__(self):        pass    def __call__(self, *args, **kwargs):        print "__call__"    def __str__(self):        return "C Class"obj = Foo()print obj #调用__str__方法
0 0