12

When should I use __init__ and when __call__ method ?

I am confused about whether should I use the first or the second.

At the moment I can use them both, but I don't know which is more appropriate.

JeremyL
  • 347
  • 1
  • 3
  • 8
  • 2
    Well, what are you trying to do? – arshajii Dec 31 '12 at 16:34
  • You can use them both, and implement them both. If you won't tell us what you are trying to do, how can we help? Did you find the documentation confusing? Which part confused? – David Heffernan Dec 31 '12 at 16:35
  • 2
    related : http://stackoverflow.com/questions/9663562/what-is-difference-between-init-and-call-in-python – Ashwini Chaudhary Dec 31 '12 at 16:36
  • Possible duplicate of [what is difference between \_\_init\_\_ and \_\_call\_\_ in python?](http://stackoverflow.com/questions/9663562/what-is-difference-between-init-and-call-in-python) – wombatonfire Jan 14 '17 at 11:43

4 Answers4

21

These two are completely different.

__init__() is the constructor, it is run on new instances of the object.

__call__() is run when you try to call an instance of an object as if it were a function.

E.g: Say we have a class, Test:

a = Test() #This will call Test.__init__() (among other things)
a() #This will call Test.__call__()
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • 1
    Except when they are made to be the same... I build lazy string Tokenizer classes that are callable iterables. To keep the implementation DRY and provide the user with equivalent APIs, `__call__()` returns `self`, and `__init__()` calls `__call__()` with the same arguments. Imagine `tokens = list(Tokenizer(doc, ngrams=2))` same as `tokenizer = Tokenizer(); tokens = list(tokenizer(doc, ngrams=2))` same as `list(tokenizer.tokenize(doc, ngrams=2))` It "just works" no matter how the user likes to use it. – hobs Jan 19 '16 at 21:14
  • @hobs, great example! Why don't you put it as an answer? – wombatonfire Jan 14 '17 at 11:52
  • As to @hobs suggestion, It's worth noting that is a pretty rare use case. In general, I can't think of many situations you want that behaviour - a single consistent way to use something is generally better than having lots of ways as it makes code easier to read. – Gareth Latty Jan 15 '17 at 16:48
  • @GarethLatty Good point. Not very pythonic. Only do what I suggest if you have a very good reason. I just did it to imitate the NLTK package. Long-lived packages with a lot of API updates tend to have a lot of ways to do the same thing, especially for callable classes like Tokenizers that need to be passed around as configured objects, called as functions within pipelines, pickled/depickled in configured form, passed as arguments to things that expect callables, etc. – hobs Jan 16 '17 at 04:21
9

A quick test shows the difference between them

class Foo(object):
    def __init__(self):
        print "init"
    def __call__(self):
        print "call"

f = Foo()  # prints "init"
f()        # prints "call"

In no way are these interchangeable

Eric
  • 95,302
  • 53
  • 242
  • 374
7

Most likely, you want to use __init__. This is the method used to initialize a new instance of your class, which you make by calling the class. __call__ is in case you want to make your instances callable. That's not something frequently done, though it can be useful. This example should illustrate:

>>> class C(object):
...   def __init__(self):
...     print 'init'
...   def __call__(self):
...     print 'call'
... 
>>> c = C()
init
>>> c()
call
>>> 
Phil Frost
  • 3,668
  • 21
  • 29
2

A simple code snippet will elaborate this better.

>>> class Math:
...     def __init__(self):
...             self.x,self.y=20,30
...     def __call__(self):
...             return self.x+self.y
... 
>>> m=Math()
>>> m()
50
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Premkumar chalmeti
  • 800
  • 1
  • 8
  • 23