0
repr method in python
Can anyone explain what is __repr__ and when and how to use it? I can't understand nothing about it.
2 Answers
+ 4
class a:
def __str__(self):
return 'a'
def __repr__(self):
return 'A'
print(a()) #a
print(repr(a())) #A
print(str(a())) #a
>>> class a:
... def __repr__(self): return 'A'
...
>>> a()
A
+ 4
__str__ is called when you print a variable
__repr__ is called in interactive mode