Python __call__ special method practical example

后端 未结 14 1682
感动是毒
感动是毒 2020-11-28 00:28

I know that __call__ method in a class is triggered when the instance of a class is called. However, I have no idea when I can use this special method, because

14条回答
  •  盖世英雄少女心
    2020-11-28 00:57

    Class-based decorators use __call__ to reference the wrapped function. E.g.:

    class Deco(object):
        def __init__(self,f):
            self.f = f
        def __call__(self, *args, **kwargs):
            print args
            print kwargs
            self.f(*args, **kwargs)
    

    There is a good description of the various options here at Artima.com

提交回复
热议问题