1

I see that the __str__ and __repr__ are being called when I try to instantiate a class. I see in lots of documentation that __str__ is called during print operations and __repr__ functions similar to __str__. I see that when I try to create an object these methods are being called. Can someone make me understand what is happening?

class A:
    print "You are in Class A"
    #The init method is a special method used when instantiating a class
    #Python looks for this function if its present in the class the arguments passed during the   instantiation of a class
    #is based on the arguments defined in this method. The __init__ method is executed during instantiation

    def __init__(self):
        print self
        print "You aer inside init"

    #Once the instance of a class is created its called an object. If you want to call an object like a method
    #say x=A() here x is the object and A is the class
    #Now you would want to call x() like a method then __call__ method must be defined in that class
    def __call__(self):
        print "You are inside call"

    #The __str__ is called when we use the print function on the instance of the class
    #say x=A() where x is the instance of A. When we use print x then __str__ function will be called
    #if there is no __str__ method defined in the user defined class then by default it will print
    #the class it belongs to and also the memory address
    def __str__(self):
        print "You are in str"
        return "You are inside str"

    #The __repr__ is called when we try to see the contents of the object
    #say x=A() where x is the instance of the A. When we use x it prints a value in the interpreter
    #this value is the one returned by __repr__ method defined in the user-defined class
    def __repr__(self):
        print "You are in repr"
        return "This is obj of A"

class B:
    print "You are in Class B"

epradne@eussjlx8001:/home/epradne>python -i classes.py
You are in Class A
You are in Class B
>>> a=A() <-------- I create the object a here
You are in str <------- Why is __str__ method executed here??
You are inside str
You aer inside init
>>> A() <------------- I just call the class and see both __str__ and __repr__ are being executed
You are in str   
You are inside str
You aer inside init
You are in repr
This is obj of A
>>>                      
Marcellinov
  • 311
  • 7
  • 18
Pradeep
  • 619
  • 2
  • 10
  • 22
  • 1
    Maybe this post help you deeply understand whats going on: [Difference between __str__ and __repr__ in Python](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python) – Lucas Godoy Oct 26 '14 at 19:35
  • Thanks lucas i will go through this. – Pradeep Oct 26 '14 at 20:01

1 Answers1

3

The following is the reason __str__() is called when you create an instance of your class:

def __init__(self):
    print self
    ^^^^^^^^^^ THIS

Here, Python needs to print the object. To do this, it converts the object to string (by calling __str__()) and prints out the resulting string.

On top of that, you see __repr__() called in in the second example because the interactive shell tries to print out the result of A().

NPE
  • 486,780
  • 108
  • 951
  • 1,012