I'm creating a class for a custom data structure, and I'd like it to mimic the behavior of a pandas dataframe object. I want to visualize my object in a way that Pandas does it - by simply printing the object itself. When you create a pandas dataframe object, it can be easily printed with nice formatting:
>>> import pandas as pd
>>> df = pd.DataFrame([1,2,3],[1,2,3])
>>> print(df)
0
1 1
2 2
3 3
I assume this implicitly accesses some attribute or calls some method, which prints said attribute somehow. I know that a __str__
method exists, which is able to return a string when called, however pd.DataFrame does not seem to implement this method, or at least I wasn't able to find it in the source code - how does it do it then?
If I were to use __str__
, I could of course concatenate my data structure's rows as strings, into a long string with new lines and return it. I can also return an empty string (since __str__
cannot return None), and print my data structure in another way inside of __str__
.
Both of those solutions do work, but they both seem kind of weird/counterintuitive. Perhaps there is a better way to do this, similar to how libraries such as Pandas handle it? Is there a known standard or best practice in this regard?
EDIT:
Possibly related question - how does the same process happen inside Jupyter Notebooks, when df
is called? Since it doesn't simply print it, but instead display it in a nicely formatted way - how would I define such behavior for my object?