Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Making the Linked List Iterable

Avatar image for Aashay

Aashay on June 1, 2025

How does print(node) prints the node’s data value?

All we have is a .__repr__() method defined which prints all the nodes, as mentioned useful for debugging!

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on June 3, 2025

@Aashay The print() function calls str() on the object passed as an argument in order to convert it into a string representation. Under the hood, str() calls the object’s .__str__() method, and when that method can’t be found, Python falls back to calling .__repr__(), as shown here:

>>> class Person:
...     def __init__(self, name):
...         self.name = name
...     
...     def __repr__(self):
...         return f"Person(name={self.name!r})"
...

>>> person = Person("John")

>>> print(person)
Person(name='John')

>>> str(person)
"Person(name='John')"

>>> repr(person)
"Person(name='John')"

Become a Member to join the conversation.