repr () trả về mô tả chi tiết để người lập trình bảo trì và sửa lỗi. str () trả về mô tả đơn giản cho người dùng sử dụng

Nguồn:: Real Python, When Should You Use .__repr__() vs .__str__() in Python? – Real Python

I was advised to not use vars() or __dict__, just use the actual API of the classes. If an object isn’t printing well without that introspection, then that’s the devs fault for not writing proper string and print methods (usually via __repr__). Or in other words, Python defers the responsibility to display the objects to the dev, while JS takes that responsibility.
Nguồn:: Is it correct that Python does not encourage us to read objects’s content?
Trong REPL, gọi trực tiếp vật thể ra thì kết quả là repr(). Nếu dùng print thì kết quả là str()
Nếu lớp không định nghĩa cả repr() và str() thì kết quả trả về có dạng main.Class_name object at 0x1025c4ed0


Cập nhật lần cuối : 30 tháng 6, 2024
Tạo : 12 tháng 10, 2023