What is the use of __Str()__ method in Python class?
when declaring a python class, implementing a __str()__ is good practice , because it is used to read the object instance of the class in a human readable format. Also we can say when the object has to be represented as a string, the Python uses the special method called __str__.
Ex:
Here we declare a Person class and then initiate an object of Person class using __init__ method. Now if we print its object, we will get a output of object memory location.
Now if we implement an __str__() method in the Person class which help us to get a human-readable string of Person object.
No comments:
Post a Comment