0

What is difference __repr__ and __str__?

2nd Dec 2018, 8:20 AM
Владислав Борисов
Владислав Борисов - avatar
5 Answers
+ 2
String is readable and repr is unambiguous. reor is more for debugging purposes. In my example you can copy the repr and actually run it in the console to recreate the datetime object.
2nd Dec 2018, 8:44 AM
James
James - avatar
+ 2
If you have __repr__ in your class then you can print your object in interpreter session also in IDLE but if you define __str__ in class and don't define __repr__ in class then you can only print your object in IDLE not in interpreter session. mostly __repr__ is debugging aid. Also watch this. https://youtu.be/aIdzBGzaxUo
2nd Dec 2018, 9:24 AM
Maninder $ingh
Maninder $ingh - avatar
+ 1
import datetime today = datetime.date.today() print(str(today)) print(repr(today))
2nd Dec 2018, 8:41 AM
James
James - avatar
+ 1
Thanks)
2nd Dec 2018, 8:47 AM
Владислав Борисов
Владислав Борисов - avatar
0
It's cool. Thanks!
2nd Dec 2018, 9:29 AM
Владислав Борисов
Владислав Борисов - avatar