__call__ when calling a method inside the class

Tharanga Abeyseela

I'm trying to understand the meaning of __call__ (python3) . Wrote this to differentiate each method __init__ , __call__, and test method.

#!/usr/bin/python3

class thara(object):

   def __init__(self):
        print("init called")

   def __call__(self):
       print("call called")

   def test(self):
       print("test called")

x=thara()  ### constructor calling here
x()   ## __call__  calling here
x.test() ## test method calling here

my question is when i initiate the x.test(), why it is not calling __call__ ? what I'm thinking is, if i initiate the x.test() will initiate the instance x(), and it should call the __call__ method automatically.But according to my output __call__ will call only when initiate x().

can someone please explain.

Dawid Gosławski

https://docs.python.org/2/reference/datamodel.html#object.__call__

__call__ is called when instance is called like a function. And this is what you do with x(). x.test() is calling method of instance, not instance itself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a static method inside a class in jar file

From Java

Calling private method inside private class inside Inner class

From Java

Calling a method inside another method in same class

From

call a static method inside a class?

From Dev

calling static method from inside the class

From Dev

NameError: global name not defined when calling method inside class

From Dev

How to call my concrete class method when I keep calling the base class method

From Dev

calling a method inside a class-Python

From Dev

TypeError when testing static method call inside class method

From Dev

How can I tell if a class has a method `__call__`?

From Dev

When the Python __call__ method gets extra first argument?

From Dev

Calling class method inside string format

From Dev

Call a function inside a class when calling the class in python

From Dev

Calling a method inside a method

From Dev

Python says my class has no __call__ method

From Dev

Calling a method from inside of another class

From Dev

Call method inside adapter Class

From Dev

Android Crash when calling a method inside a class, without trigger exceptions or errors

From Dev

MissingMethodException when calling method inside another

From Dev

Calling a method inside fragment class using the Activity class

From Dev

Calling an inherited class method decorator inside child class

From Dev

Problem with the call to method inside the class

From Dev

Calling a method inside a method of a different class C++

From Dev

UnboundLocalError: When calling a class method

From Dev

Calling Methods Inside Another Method In a Different Class

From Dev

calling an abstract method in a concrete method inside an abstract class in C#

From Dev

Calling Attributes from Class inside of a Class Method, Code Error

From Dev

Calling a method from another method inside the same class using getattr

From Dev

Why does calling a virtual class's virtual method inside std::optional generate a call to the base method, not an overridden?

Related Related

  1. 1

    Calling a static method inside a class in jar file

  2. 2

    Calling private method inside private class inside Inner class

  3. 3

    Calling a method inside another method in same class

  4. 4

    call a static method inside a class?

  5. 5

    calling static method from inside the class

  6. 6

    NameError: global name not defined when calling method inside class

  7. 7

    How to call my concrete class method when I keep calling the base class method

  8. 8

    calling a method inside a class-Python

  9. 9

    TypeError when testing static method call inside class method

  10. 10

    How can I tell if a class has a method `__call__`?

  11. 11

    When the Python __call__ method gets extra first argument?

  12. 12

    Calling class method inside string format

  13. 13

    Call a function inside a class when calling the class in python

  14. 14

    Calling a method inside a method

  15. 15

    Python says my class has no __call__ method

  16. 16

    Calling a method from inside of another class

  17. 17

    Call method inside adapter Class

  18. 18

    Android Crash when calling a method inside a class, without trigger exceptions or errors

  19. 19

    MissingMethodException when calling method inside another

  20. 20

    Calling a method inside fragment class using the Activity class

  21. 21

    Calling an inherited class method decorator inside child class

  22. 22

    Problem with the call to method inside the class

  23. 23

    Calling a method inside a method of a different class C++

  24. 24

    UnboundLocalError: When calling a class method

  25. 25

    Calling Methods Inside Another Method In a Different Class

  26. 26

    calling an abstract method in a concrete method inside an abstract class in C#

  27. 27

    Calling Attributes from Class inside of a Class Method, Code Error

  28. 28

    Calling a method from another method inside the same class using getattr

  29. 29

    Why does calling a virtual class's virtual method inside std::optional generate a call to the base method, not an overridden?

HotTag

Archive