Default __str__ method for an instance

余生长醉 提交于 2019-12-11 15:27:19

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/58310753/default-str-method-for-an-instance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!