Why do we have callable objects in python?

后端 未结 3 672
感情败类
感情败类 2020-12-16 00:53

What is the purpose of a callable object? What problems do they solve?

3条回答
  •  一生所求
    2020-12-16 01:46

    They take parameters and return a result depending on those parameters.

    A callable is just an abstract form of a function resp an interface that defines that an object acts like a function (i.e. accepts parameters).

    As functions are first class objects, it is obvious that functions are callable objects. If you are talking about the __call__ method, this is just one of the many special methods with which you can overload the behavior of custom objects, e.g. for arithmetic operations or also defining what happens if you call an object.

    One idea why to use such is to have some kind of factory object that itself creates other objects.

提交回复
热议问题