1

Just started learning python, I've come across __call__ method. I understand when __init__ gets called and when __call__ gets called by running a simple example:

class Constructor:
    def __init__(self, a):
        print "__init__ called"

    def __call__(self):
        print "__call__ called"

    def dummy(self):
        print "a dummy method called"

print "creating x obj"
x = Constructor(1)
print "calling x object"
x()

console output:

creating x obj
__init__ called
calling x object
__call__ called

My questions are:

  1. I come from java background, I know __init__ maps to java constructor, does java have an equivalent to __call__ to help me understand this better?
  2. Can someone give me a simple example of when __call__ can be useful?
4
  • I think what you might use is the Runnable interface: docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html it is very simple, just a run() method. Build an object that implements the runnable interface and then call x.run(). Interfaces (like this, or your own) make the Observer pattern possible, which IMO is the single most important pattern for writing reusable objects. en.wikipedia.org/wiki/Observer_pattern Commented Mar 18, 2013 at 10:56
  • This is where I get confused, if you can simple do x.run() by defining a simple method called run() in the python class. Why python bothers at all to design something like __ call __?
    – Shengjie
    Commented Mar 18, 2013 at 10:59
  • 2
    In python __call__ is a base of the language (see eli.thegreenplace.net/2012/03/23/…) and helpfull to solving several problems/tasks like observer pattern, callback and so on. Commented Mar 18, 2013 at 11:19
  • It is also a way to replace a function with a configurable class object at a later time, without changing the code where the function was called
    – Lars
    Commented Oct 21, 2019 at 13:42

2 Answers 2

5

__call__ equates to overloading the () operator on a class. This is possible in C++ but not Java. It allows one to have function objects (sometimes called functors).

In Python these are termed callable objects.

In simple terms, this allows you to treat an object as a function.

The actual use for such concepts is beyond the scope of this answer but there is plenty of information around the internet. For example Wikipedia.

6
  • In Python they're called callable objects, not functors. Functor already means something else in computer science anyway. Commented Mar 18, 2013 at 10:50
  • I think Functor and Function Object and Callable Object all mean the same thing - there may of course be other meanings of each.
    – Nick
    Commented Mar 18, 2013 at 10:52
  • Well, the main meaning of "functor" (from category theory) is very important. But I think the term "functor" for callable object is only really used within the C++ community. You won't find the term "functor" in the Python documentation anywhere, but "callable" is common. Commented Mar 18, 2013 at 10:55
  • @Nick I get the point now, can you give an example where callable object might be useful. How it's different from a simple method?
    – Shengjie
    Commented Mar 18, 2013 at 10:58
  • @Shengjie: It isn't different, except for the syntax. Commented Mar 18, 2013 at 11:02
3

Nope, there is nothing like __call__ in Java. You cannot use an object as a method call. You have to create a method and call it:

class Constructor{
    Constructor(){
        System.out.println("Constructor called");
    }

    void doStuff(){
        System.out.println("do called");
    }

    public static void main(String[] args){
        System.out.println("Creatng x obj");
        x = new Constuctor();
        System.out.println("Calling x obj");
        x.doStuff();
    }
}
1
  • so in your example, doStuff pretty much plays the role of 'call' here.
    – Shengjie
    Commented Mar 18, 2013 at 10:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.