I will give a slightly expanded answer. In Python, functions are first-class objects . This means that they can be "dynamically created, destroyed, transferred to functions, returned as values, and have all rights, like other variables in the programming language."
Calling an instance of a function / class in Python means calling the __call__ method of this object. For old-style classes, instance instances are also called, but only if the object that creates them has the __call__ method. The same applies to new-style classes, with the exception of the term "instance" with new-style classes. Rather, these are "types" and "objects."
As stated in the Python 2 data model page , for function objects, class instances (old-style classes) and class objects (new -style classes), " x(arg1, arg2, ...) is short for x.__call__(arg1, arg2, ...) ".
Thus, whenever you define a function with the shorthand def funcname(parameters): you really just create the object using the __call__ method, and the shorthand for __call__ is just the instance name and follow it with parentheses containing the call arguments. Since functions are first-class objects in Python, you can create them on the fly with dynamic parameters (and therefore accept dynamic arguments). This will come in handy for the functions / classes of decorators, which you will read about later.
I currently suggest reading the Official Python Guide .
source share