Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have callable objects in python?

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

like image 824
StackUnderflow Avatar asked Mar 12 '10 23:03

StackUnderflow


People also ask

What does it mean for an object to be callable in Python?

A callable object is an object which can be used and behaves like a function but might not be a function. By using the __call__ method it is possible to define classes in a way that the instances will be callable objects. The __call__ method is called, if the instance is called "like a function", i.e. using brackets.

What does it mean when an object is not callable in Python?

The “int object is not callable” error occurs when you declare a variable and name it with a built-in function name such as int() , sum() , max() , and others. The error also occurs when you don't specify an arithmetic operator while performing a mathematical operation.

What is a callable type in Python?

Short version, Callable is a type hint that indicates a function or other object which can be called. Consider a simple example below. The bar parameter is a callable object that takes two ints as parameters and returns an int.

What does __ call __ do in Python?

__call__ in Python The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.

Is lambda a callable Python?

Are Lambda functions callable? Hardly surprising, a Lambda function in Python is callable.


1 Answers

Many kinds of objects are callable in Python, and they can serve many purposes:

  • functions are callable, and they may carry along a "closure" from an outer function
  • classes are callable, and calling a class gets you an instance of that class
  • methods are callable, for function-like behavior specifically pertaining to an instance
  • staticmethods and classmethods are callable, for method-like functionality when the functionality pertains to "a whole class" in some sense (staticmethods' usefulness is dubious, since a classmethod could do just as well;-)
  • generators are callable, and calling a generator gets you an iterator object
  • finally, and this may be specifically what you were asking about (not realizing that all of the above are objects too...!!!), you can code a class whose instances are callable: this is often the simplest way to have calls that update an instance's state as well as depend on it (though a function with a suitable closure, and a bound method, offer alternatives, a callable instance is the one way to go when you need to perform both calling and some other specific operation on the same object: for example, an object you want to be able to call but also apply indexing to had better be an instance of a class that's both callable and indexable;-).

A great range of examples of the kind of "problems they solve" is offered by Python's standard library, which has many cases of each of the specific types I mention above.

like image 92
Alex Martelli Avatar answered Sep 29 '22 01:09

Alex Martelli