J
J
Julia2014-01-17 22:02:00
Python
Julia, 2014-01-17 22:02:00

What is the difference between __init__ and __call__ ?

What is the difference between __init__ and __call__ ?
Where can I read about them in Russian?
And how they in general are called - methods?
Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ali Aliyev, 2014-01-18
@ali_aliev

__init__ is a normal constructor, __call__ is a call to an object as a function. Initially, you need to understand that everything in python is an object, including functions, so functions also have a __call__ method. This fact is very easy to verify:

>>> def name():
...     pass
...
>>> dir(name)
['__call__', ...

If you create a class with a __call__ method and create an object, your object will "turn" into a function. For example:
>>> class Name(object):
...     def __call__(self, first, second):
...         return first + second
...
>>> f = Name()
>>> f(1,2)
3
>>>

There are no magic methods in python (as they are usually called in php). There are, for example, methods for operator overloading (__getitem__, __setitem__, __index__), iteration protocol (__iter__, __next__), context manager protocol (__exit__, __enter__), managed attributes (__getattr__, __setattr__, __slots__, __getattribute__), descriptor protocols (__get__, __set__, __delete__) and so on. :)
PS as always I recommend Lutz, everything is very detailed there

L
leclecovich, 2014-01-17
@leclecovich

stackoverflow.com/questions/9663562/what-is-diff...
docs.python.org/2/reference/datamodel.html#object....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question