Monday, September 21, 2020

 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

Element wise operation on LIST vs ARRAY

The use of arrays over lists: You can write  vectorised  code on numpy arrays, not on lists, which is  convenient to read and write, and con...