What is a “callable”?

前端 未结 12 2299
谎友^
谎友^ 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:18

    A Callable is an object that has the __call__ method. This means you can fake callable functions or do neat things like Partial Function Application where you take a function and add something that enhances it or fills in some of the parameters, returning something that can be called in turn (known as Currying in functional programming circles).

    Certain typographic errors will have the interpreter attempting to call something you did not intend, such as (for example) a string. This can produce errors where the interpreter attempts to execute a non-callable application. You can see this happening in a python interpreter by doing something like the transcript below.

    [nigel@k9 ~]$ python
    Python 2.5 (r25:51908, Nov  6 2007, 15:55:44) 
    [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 'aaa'()    # <== Here we attempt to call a string.
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'str' object is not callable
    >>> 
    

提交回复
热议问题