If you actually ran that in Java, I think you'd find it probably prints out true because of string interning, but that's somewhat irrelevant.

I'm not sure what you mean by "replaces it with the object it is referring to". What actually happens is that when you write a == b, Python calls a.__eq__(b), which is just like any other method call on a with b as an argument.

If you want an equivalent to Java-like ==, use the is operator: a is b. That compares whether the name a refers to the same object as b, regardless of whether they compare as equal.

Answer from Danica on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › print-objects-of-a-class-in-python
Print Objects of a Class in Python - GeeksforGeeks
July 12, 2025 - print([t]) invokes __repr__() because Python uses __repr__ to represent objects within containers like lists.
Discussions

django - Find all references to an object in python - Stack Overflow
What is a good way to find all of the references to an object in python? The reason I ask is that it looks like we have a "memory leak". We are uploading image files to the server from a web brows... More on stackoverflow.com
🌐 stackoverflow.com
How to get an object name?
What happens if you write r = t = cls()? Should the name be r or t? What if you write my_list.append(cls())? You never assigned a name, you just put it in a list. Or you could write print(cls().getName()). Here, you never even save the resulting object. Objects do not have unique names, so there is no general way to do this. In some cases you can use f-string debugging to show the way you got the object, but that's not always a simple variable/name. >>> import math >>> x = 1 >>> print(f'{x=}, {math.sin(x)=}') x=1, math.sin(x)=0.8414709848078965 More on reddit.com
🌐 r/learnpython
26
2
January 31, 2023
python - How to print instances of a class using print()? - Stack Overflow
If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for printing. ... there's also a unicode method, which you can use instead of Str ; note that it should return a unicode object, not a string (but if you return a string, the conversion ... More on stackoverflow.com
🌐 stackoverflow.com
oop - Python - printing out all references to a specific instance - Stack Overflow
Try Finding objects' names, it prints all names that reference a given object. More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 4
49

It represents an infinite loop within the structure. An example:

In [1]: l = [1, 2]

In [2]: l[0] = l

In [3]: l
Out[3]: [[...], 2]

l's first item is itself. It's a recursive reference, and so python can't reasonably display its contents. Instead it shows [...]

2 of 4
31

Depending on the context here it could different things:

indexing/slicing with Ellipsis

I think it's not implemented for any python class but it should represent an arbitary number of data structure nestings (as much needed). So for example: a[..., 1] should return all the second elements of the innermost nested structure:

>>> import numpy as np
>>> a = np.arange(27).reshape(3,3,3)  # 3dimensional array
>>> a[..., 1]  # this returns a slice through the array in the third dimension
array([[ 1,  4,  7],
       [10, 13, 16],
       [19, 22, 25]])
>>> a[0, ...]  # This returns a slice through the first dimension
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

and to check for this ... you compare it to an Ellipsis (this is a singleton so recommended is using is:

>>> ... is Ellipsis
True
>>> Ellipsis in [...]
True
# Another (more or less) equivalent alternative to the previous line:
>>> any(i is Ellipsis for i in [1, ..., 2]) 
True

Recursive Datastructures

The other case in which you see an [...] in your output is if you have the sequence inside the sequence itself. Here it stands for an infinite deeply nested sequence (that's not printable). For example:

>>> alist = ['a', 'b', 'c']
>>> alist[0] = alist
>>> alist
[[...], 'b', 'c']

# Infinite deeply nested so you can use as many leading [0] as you want
>>> alist[0][1] 
'b'
>>> alist[0][0][0][0][0][1] 
'b'
>>> alist[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][1] 
'b'

You can even replace it several times:

>>> alist[2] = alist
>>> alist
[[...], 'b', [...]]
>>> alist[1] = alist
>>> alist
[[...], [...], [...]]

To test if you have any such recursion in your output you need to check if the data-structure itself is also one of the elements:

>>> alist in alist
True
>>> any(i is alist for i in alist)
True

Another way to get a more meaningful output is using pprint.pprint:

>>> import pprint
>>> pprint.pprint(alist)  # Assuming you only replaced the first element:
[<Recursion on list with id=1628861250120>, 'b', 'c']
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python references
Python References
March 27, 2025 - When you access the counter variable, Python looks up the object referenced by the counter and returns the value of that object: print(counter) # 100Copy · So variables are references that point to the objects in the memory. To find the memory address of an object referenced by a variable, you pass the variable to the built-in id() function.
🌐
W3Schools
w3schools.com › python › ref_func_print.asp
Python print() Function
The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
🌐
FavTutor
favtutor.com › blogs › print-object-attributes-python
How to Print Object Attributes in Python? (with Code)
October 14, 2022 - To print all the attributes of an object in Python, you can use the 'getmembers()' function of the inspect module. This function returns the list of tuples containing the attributes along with their values.
🌐
Python documentation
docs.python.org › 3 › reference › datamodel.html
3. Data model — Python 3.14.8 documentation
Called by str(object), the default __format__() implementation, and the built-in function print(), to compute the “informal” or nicely printable string representation of an object.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › print-objects-of-a-class-in-python
Print objects of a class in Python
April 2, 2026 - class Person: def __init__(self, ... object at 0x0000029D0DAD7250> <__main__.Person object at 0x0000029D0DBEF7F0> The __repr__ method provides a detailed string representation primarily for developers and debuggi...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to get the current reference count of an object in python?
How to Get The Current Reference Count of an Object in Python? - Be on the Right Side of Change
December 20, 2020 - Question: How to get the current reference count of a Python object? Answer: You can get the current reference count with the sys module’s getrefcount() method. For example, to get the number of times variable x is referenced, run sys.getrefcount(x). Here’s the general strategy for an arbitrary object: import sys print(sys.getrefcount(object)) The following code snippet shows an example of an object x that has a reference count of 13.
🌐
Python
docs.python.org › 3 › library › pprint.html
pprint — Data pretty printer
The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable.
🌐
Medium
medium.com › @officialyrohanrokade › simplified-pythons-reference-handling-understanding-object-referencing-deleting-references-and-ae4026a74d7b
Simplified Python’s Reference Handling: Understanding Object Referencing, Deleting References, and Shallow Copying vs. Deep Copying | by Rohan Rokade | Medium
December 8, 2023 - # Original list original_list = [1, 2, 3] # Create a new reference to the original list new_reference = original_list # Modifying the new reference new_reference.append(4) print(original_list) # Output: [1, 2, 3, 4] print(new_reference) # Output: [1, 2, 3, 4] # Deleting the reference - the 'magic trick' del new_reference # The original list still exists print(original_list) # Output: [1, 2, 3, 4] In Python, every object has a reference count, a number keeping track of how many variables point to it.
Top answer
1 of 13
973
>>> class Test:
...     def __repr__(self):
...         return "Test()"
...     def __str__(self):
...         return "member of Test"
... 
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test

The __str__ method is what gets called 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 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.

2 of 13
192

As Chris Lutz explains, this is defined by the __repr__ method in your class.

From the documentation of repr():

For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.

Given the following class Test:

class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __repr__(self):
        return f"<Test a:{self.a} b:{self.b}>"

    def __str__(self):
        return f"From str method of Test: a is {self.a}, b is {self.b}"

..it will act the following way in the Python shell:

>>> t = Test(123, 456)
>>> t
<Test a:123 b:456>
>>> print(repr(t))
<Test a:123 b:456>
>>> print(t)
From str method of Test: a is 123, b is 456
>>> print(str(t))
From str method of Test: a is 123, b is 456

If no __str__ method is defined, print(t) (or print(str(t))) will use the result of __repr__ instead

If no __repr__ method is defined then the default is used, which is roughly equivalent to:

def __repr__(self):
    cls = self.__class__
    return f"<{cls.__module_}.{cls.__qualname__} object at {id(self)}>"
🌐
Daniel Michaels
danielms.site › blog › python-object-references
Daniel Michaels | Python Object References
July 9, 2018 - An object’s identity never changes once it has been created. However its values might, and generally this is what we care about more. Python gives us the option to check either like so: a == b # compares the values a is b # compares the identities · Lets extend this using a more complex example using some dictionaries. batman = {'name': 'Bruce Wayne', 'job': 'crime fighter'} bruce = batman print(batman == bruce) # True print(batman is bruce) # True
🌐
Delft Stack
delftstack.com › home › howto › python › print object python
How to Print an Object of a Class in Python | Delft Stack
February 2, 2024 - When we print an object in Python using the print() function, the object’s __str__() method is called. If it is not defined then the __str__() returns the return value of the __repr__() method.
🌐
Edureka Community
edureka.co › home › community › categories › python › how to print objects of class using print...
How to print objects of class using print function in Python | Edureka Community
December 3, 2018 - Beginning to learn ropes in Python. When I try to print an object of class Foobar using the ... achieve this easily in Python? Appreciate some help.
🌐
YouTube
youtube.com › watch
Object References & Object Identity - Variables in Python - Python Tutorial for Beginners - YouTube
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!🖥️ Variables in Py...
Published: February 6, 2021