Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (1.6k points)
Now that it's clear what a metaclass is, there is an associated concept that I use all the time without knowing what it really means.

I suppose everybody made once a mistake with parenthesis, resulting in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.

Could you give me some explanations, including examples with the magic method ?

1 Answer

0 votes
by (25.1k points)

A callable is anything that can be called, like a method or a constructor or anything with an __call__ method. e.g.

class Example:

    def __call__(self):

        print("I  was called.")

obj = Example();

obj() # will print "I  was called."

Browse Categories