What is a “callable”?

前端 未结 12 2101
谎友^
谎友^ 2020-11-22 01:23

Now that it\'s clear what a metaclass is, there is an associated concept that I use all the time without knowing what it really means.

I suppose everybody made once

12条回答
  •  失恋的感觉
    2020-11-22 02:10

    __call__ makes any object be callable as a function.

    This example will output 8:

    class Adder(object):
      def __init__(self, val):
        self.val = val
    
      def __call__(self, val):
        return self.val + val
    
    func = Adder(5)
    print func(3)
    

提交回复
热议问题