简体   繁体   中英

Default __str__ method for an instance

I have a class without a str or repr method. And when I call it:

>>> from ingest.tpr import TPR
>>> t=TPR()
>>> t # that is, "repr(t)"
<ingest.tpr.TPR instance at 0x101eb8518>

The default representation here seems to be something like:

"<${path_to_class} instance at ${memory_address}>"

Where is this default implementation supplied? And is the above the __repr__ method or the __str__ method -- I know it calls the __repr__ method in the above, but are both the __str__ and __repr__ methods initialized to the same thing at the start, or does one simply call the other as a "fall-back". In other words, what does the default __str__ and __repr__ look like in python and where would that be defined?

t.__repr__ (which is what repr(t) calls) resolves to (assuming no other upstream definitions) object.__repr__ , which is defined by the Python implementation (for example, https://github.com/python/cpython/blob/master/Objects/typeobject.c#L3827 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM