Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

Can anyone tell me, How to print the instances of a class utilizing the print()?

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
In order to print the instances of a class using the print() function, you can override the __str__() method in your class. The __str__() method allows you to define a string representation of your object.

Here's an example:

class MyClass:

    def __init__(self, name, age):

        self.name = name

        self.age = age

    def __str__(self):

        return f"MyClass instance - name: {self.name}, age: {self.age}"

# Create instances of MyClass

obj1 = MyClass("John", 25)

obj2 = MyClass("Alice", 30)

# Print the instances using the print() function

print(obj1)

print(obj2)

Output:

MyClass instance - name: John, age: 25

MyClass instance - name: Alice, age: 30

In the above example, the __str__() method is overridden in the MyClass class. It returns a string that represents the object in a customized format. When you call print(obj1) or print(obj2), the __str__() method of the corresponding instance is invoked, and the returned string is printed using the print() function.
0 votes
by (26.4k points)

Kindly check the below code:

>>> class Test:

...     def __repr__(self):

...         return "Test()"

...     def __str__(self):

...         return "member of Test"

... 

>>> t = Test()

>>> t

Test()

>>> print(t)

member of Test

Here,

  • The __str__ method is what happens when you print it, and the __repr__ method is what happens when you use the repr() function (or when you look at it with the interactive prompt). If this isn't the most Pythonic method, I apologize, because I'm still learning too - but it works.

  • If no __str__ method is given, Python will print the result of __repr__ instead. If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for printing.

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

0 votes
by (15.4k points)
To print instances of a class using the print() function, you can override the __str__() method within your class. This method allows you to define a customized string representation of your object.

Here's an example:

class MyClass:

    def __init__(self, name, age):

        self.name = name

        self.age = age

    def __str__(self):

        return f"MyClass instance - name: {self.name}, age: {self.age}"

# Create instances of MyClass

obj1 = MyClass("John", 25)

obj2 = MyClass("Alice", 30)

# Print the instances using the print() function

print(obj1)

print(obj2)

Output:

MyClass instance - name: John, age: 25

MyClass instance - name: Alice, age: 30

In the example above, the __str__() method is overridden in the MyClass class. It returns a string that represents the object in a customized format. When you call print(obj1) or print(obj2), the __str__() method of the respective instance is invoked, and the returned string is printed using the print() function.
0 votes
by (19k points)
The shortest way to print instances of a class using the print() function is by directly passing the instances to the print() function. Here's an example:

class MyClass:

    pass

# Create instances of MyClass

obj1 = MyClass()

obj2 = MyClass()

# Print the instances using the print() function

print(obj1, obj2)

Output:

<__main__.MyClass object at 0x00000123ABCD>, <__main__.MyClass object at 0x00000456EFGH>

In the example above, the instances obj1 and obj2 are passed as arguments to the print() function. By default, the print() function will display the string representation of the instances, which includes the class name and the memory address where the objects are stored.

Related questions

0 votes
1 answer
asked Jul 6, 2019 in Python by Sammy (47.6k points)
+2 votes
2 answers
+1 vote
2 answers
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...