I would like to have a class
class Test:
def __repr__(self):
if self in a list/set:
return self.__class__.__name__
else:
return 'My pretty print like Numpy/Tensorflow'
that is able to have an "adaptive" __repr__()
. When I simply print the instance, it should print the pretty print.
a = Test()
a
'My pretty print like Tensorflow/numpy'
When I put the object in a list and print the list, it just shows the object name.
b = Test()
z = [a, b]
z
[a, b]
I need this feature because normally printing z
will give ['My pretty print like Tensorflow/numpy', 'My pretty print like Tensorflow/numpy']
which looks ugly.
My attempt: use garbage collector to know if the object is in a list?