简体   繁体   English

实例的默认 __str__ 方法

[英]Default __str__ method for an instance

I have a class without a str or repr method.我有一个没有strrepr方法的 class 。 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".上面是__repr__方法还是__str__方法——我知道它在上面调用了__repr__方法,但是__str____repr__方法是否在开始时都初始化为相同的东西,或者只是简单地调用另一个作为“倒退”。 In other words, what does the default __str__ and __repr__ look like in python and where would that be defined?换句话说,默认的__str____repr__在 python 中是什么样子的,在哪里定义?

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 ) 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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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