Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
recategorized by

I have the list of class instances, and when I call it in the Python it looks like this:

[<__main__.Animal at 0x1f053e6abc8>,

 <__main__.Animal at 0x1f049e3f848>,

 <__main__.Animal at 0x1f0532e8d08>,

 <__main__.Animal at 0x1f053e6a1c8>]

Is there any way where I could add something to the code inside the classes to make them look like this:

[Lion,

 Gorilla,

 Tiger,

 Baboon]

1 Answer

0 votes
by (36.8k points)

You can implement the __repr__ method for the class. It should return the string. If, for example, your class has the "name" attribute, you can just use:

def __repr__(self): return self.name

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...