callable()
The built-in callable()
function checks whether an object appears to be callable, which means you can use parentheses to invoke it as a function. It returns True
if the object is callable and False
otherwise:
>>> callable(abs)
True
>>> callable(None)
False
callable()
Signature
callable(object)
Arguments
Argument | Description |
---|---|
object |
The object to check |
Return Value
- Returns
True
if the object is callable. - Returns
False
if the object is not callable.
callable()
Examples
With a function as an argument:
>>> callable(max)
True
With a non-callable object:
>>> callable(42)
False
callable()
Common Use Cases
The most common use cases for the callable()
function include:
- Checking if an object is callable before invoking it to prevent runtime errors
- Validating if objects, such as commands or handlers, are callable in frameworks or libraries
callable()
Real-World Example
In an application that processes commands, ensuring that commands are callable before executing them can prevent errors. Here’s an example using a demo command processor class:
cmd.py
class CommandProcessor:
def __init__(self):
self.commands = {}
def register_command(self, command):
if not callable(command):
raise ValueError("command is not callable")
self.commands[command.__name__] = command
def execute_command(self, name, *args, **kwargs):
if (command := self.commands.get(name)) is None:
raise ValueError(f"command '{name}' not found")
return command(*args, **kwargs)
# Usage
command_processor = CommandProcessor()
def greet(name):
return f"Hello, {name}!"
command_processor.register_command(greet)
print(
command_processor.execute_command("greet", "Alice")
) # Output: Hello, Alice!
In this example, callable()
ensures that only valid, callable commands are registered, preventing runtime errors when executing commands.
callable()
in Custom Classes
You can make instances of your custom classes callable by implementing the .__call__()
method:
adder.py
class Adder:
def __init__(self, increment):
self.increment = increment
def __call__(self, value):
return value + self.increment
# Usage
add_five = Adder(5)
print(add_five(10)) # Output: 15
By defining .__call__()
, the Adder
class’s instances become callable, which allows you to use them like functions.
Related Resources
Tutorial
Python's Built-in Functions: A Complete Exploration
In this tutorial, you'll learn the basics of working with Python's numerous built-in functions. You'll explore how to use these predefined functions to perform common tasks and operations, such as mathematical calculations, data type conversions, and string manipulations.
For additional information on related topics, take a look at the following resources:
- Python's .__call__() Method: Creating Callable Instances (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Defining Your Own Python Function (Tutorial)
- Python's Built-in Functions: A Complete Exploration (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Defining and Calling Python Functions (Course)
- Defining Your Own Python Function (Quiz)