Remember
callable() checks whether an object appears callable. It does not guarantee that a particular argument list will work.
callable() checks whether an object appears callable. It does not guarantee that a particular argument list will work.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
value = 3
print(callable(value))
print(callable(print))Top-level scope
3callable() checks whether an object appears callable. It does not guarantee that a particular argument list will work.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
value = 3
print(callable(value))
print(callable(print))
|
v
DISPLAYED RESULT
False
True
FINAL BINDINGS
value --> object 1 (int)
object 1: 3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
value = 3
print(callable(value))
print(callable(print))Top-level scope
3The example has finished. Compare the final output with the values and references above.
False True
What do you expect this program to print?
value = 3
print(callable(value))
print(callable(print))callable() FunctionThe callable() function in Python checks whether an object can be called like a function. It returns True if the object appears callable (such as a function, method, class, or object with a __call__() method), otherwise it returns False.
callable(object)
object – Any Python object (function, class, variable, etc.)True if the object is callableFalse otherwisedef greet():
print("Hello!")
print(callable(greet))
True
Why? Because greet is a function, and functions are callable.
x = 42
print(callable(x))
False
Why? Because integers are not callable.
class Person:
pass
print(callable(Person))
True
Why? Because classes can be called to create new instances.
class MyClass:
def __call__(self):
print("I can be called!")
obj = MyClass()
print(callable(obj))
True
Why? Because the object defines a __call__() method.
__call__() are callable too!TypeErrorIn advanced Python coding rounds, callable objects and the __call__() method are often tested to assess your understanding of OOP and metaprogramming.
callable(obj) returns True if the object can be called like a function.__call__() are callable.Create a class called Counter that behaves like a function and prints "Counting..." when called. Use callable() to check the object.
class Counter:
def __call__(self):
print("Counting...")
c = Counter()
print(callable(c))
c()
True
Counting...
Connect what you noticed in the example with the idea below.
callable() checks whether an object appears callable. It does not guarantee that a particular argument list will work.
callable() runs the object to test it.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.