filmov
tv
Python - How to Print an Instance of a Class

Показать описание
#Python #Shorts
If you execute the print function with the instance of your class, it will print some nonsense like "object at 0x000001F4E10E0BE0" by default. That's not very useful if you're trying, for example, to debug what went wrong with your class's instance. What you could do, though, is define __str__ and/or __repr__ methods in your class and have them return a string that represents whatever you think is valuable about the instance of the class.
-------------------------------------------------------------------------------------
Links:
If you execute the print function with the instance of your class, it will print some nonsense like "object at 0x000001F4E10E0BE0" by default. That's not very useful if you're trying, for example, to debug what went wrong with your class's instance. What you could do, though, is define __str__ and/or __repr__ methods in your class and have them return a string that represents whatever you think is valuable about the instance of the class.
-------------------------------------------------------------------------------------
Links: