Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python Callable Object Code

Practice making instances callable with __call__ and using callable-object behavior.

Advanced ~9 min read

Challenge 1

Add __call__ so the instance returns a greeting.

python Your answer
class Greeter:
    

greet = Greeter()
print(greet("Mina"))

Press Ctrl⌘Enter to check your answer

Challenge 2

Increment the stored count each time the callable instance is called.

python Your answer
class Counter:
    def __init__(self):
        self.count = 0

    

counter = Counter()
print(counter())
print(counter())

Press Ctrl⌘Enter to check your answer

Challenge 3

Make the callable accept a value and multiplier and return their product.

python Your answer
class Multiplier:
    def __call__(self, value, multiplier):
        

multiply = Multiplier()
print(multiply(6, 7))

Press Ctrl⌘Enter to check your answer

Challenge 4

Override __call__ and use the base implementation through super().

python Your answer
class AddOne:
    def __call__(self, value):
        return value + 1

class DoubleThenAdd(AddOne):
    def __call__(self, value):
        

operation = DoubleThenAdd()
print(operation(4))

Press Ctrl⌘Enter to check your answer