
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Objects of a Class in Python
An object is the part of the Object Oriented Language (OOPs), which is an instance of the class. The class is the blueprint or template that specifies the methods and properties of that given class. When we create an object of a class, it contains all the set of instance methods and variables that are related to that particular object
Using __str__ method
We have two methods namely, __str__ or __repr__ in python which automatically calls the string and used to print the object with the detail view about it. Following is the syntax for using the __str__ method.
def __str__(self): return string_representation print(object_name(inputs) def __repr__(self): return string_representation print(object_name(inputs)
Example
In the following example, we are creating a class and an object using python; and in the class we will define the function along with the __str__ method by giving the formatted text in it and finally prints the object.
class Person: def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender def __str__(self): return f"My name is {self.name} and I am {self.age} years old." person1 = Person("John", 30, "male") person2 = Person("Jane", 25, "female") print(person1) print(person2)
Output
My name is John and I am 30 years old. My name is Jane and I am 25 years old.
Example
In the following example, if we omit mentioning the method, then the description of the object will be printed rather than the data of the object.
class Person: def __init__(self, language, state, gender): self.name = language self.age = state self.gender = gender person1 = Person("John", 30, "male") person2 = Person("Jane", 25, "female") print(person1) print(person2)
Output
<__main__.Person object at 0x0000029D0DAD7250> <__main__.Person object at 0x0000029D0DBEF7F0>
Example
In python we have another method named __repr__ to print the object with the detailed view. This method is as same as the __str__ method but the difference is that, it handles more details of the object.
class Person: def __init__(self, name, state, place): self.name = name self.state = state self.place = place def __repr__(self): return f"{self.name} belongs to {self.state} and the home town of {self.name} is {self.place}" person1 = Person("John", "Andhra Pradesh", "male") person2 = Person("Jane", "Telangana", "female") print(person1) print(person2)
Output
John belongs to Andhra Pradesh and the home town of John is Vishakhapatnam Jane belongs to Telangana and the home town of Jane is Hyderabad
Example
When we didn't use the __repr__ in the user defined class and if we try to print the object then the output of the print statement will be the description of the object.
class Person: def __init__(self, name, state, gender): self.name = name self.state = state self.gender = gender person1 = Person("John", "Andhra Pradesh", "male") person2 = Person("Jane", "Telangana", "female") print(person1) print(person2)
Output
<__main__.Person object at 0x0000029D0DBE7790> <__main__.Person object at 0x0000029D0DBE7CA0>