Why we use __repr__ in python classes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why we use __repr__ in python classes?

I m using __repr__ as function for clasess in python. When i create the object of the class and simply print it ,return me the value that i put in the __repr__ function. I wonder if i use another class name and print the object simply , it return me the adderss value.

2nd Apr 2019, 6:06 PM
Tony Stark
Tony Stark - avatar
1 Answer
+ 8
__repr__ is a magic method or they also call it dunders. This method is called whenever you try to print an object of a class. If you create your custom class then you must override this method in order to print an object of that class. If you're not overriding this method then printing an object will print its address in the memory.
2nd Apr 2019, 6:25 PM
Шащи Ранжан
Шащи Ранжан - avatar