python - 实例的默认 __str__ 方法
问题描述
我有一个没有str
orrepr
方法的类。当我称之为:
>>> from ingest.tpr import TPR
>>> t=TPR()
>>> t # that is, "repr(t)"
<ingest.tpr.TPR instance at 0x101eb8518>
这里的默认表示似乎是这样的:
"<${path_to_class} instance at ${memory_address}>"
这个默认实现在哪里提供?上面是__repr__
方法还是__str__
方法——我知道它调用了__repr__
上面的方法,但是方法__str__
和__repr__
方法在开始时都初始化为相同的东西,还是简单地将另一个称为“后备” . 换句话说,python 中的默认值__str__
和__repr__
外观是什么样的,在哪里定义?
解决方案
t.__repr__
(这就是repr(t)
所谓的)解析为(假设没有其他上游定义)object.__repr__
,这是由 Python 实现定义的(例如,https://github.com/python/cpython/blob/master/Objects/typeobject.c#L3827 )
推荐阅读
- apache-spark - How to solve the problem of too much data in Spark?
- ruby-on-rails - Flash Danger 消息仅在登录视图中出现两次
- php - 如何在此代码中使用 PHP preg_match 分隔符模式?
- powershell - 如何使用 powershell 在每台计算机中使 GUI 大小固定大小?
- web-scraping - 有没有办法从不提供开放 API 的网站/应用程序中检索数据
- python-3.x - 为什么 __init__() 缺少一个必需的位置参数?
- entity-framework - EF Core 2.2:使用相同的 DbContext 实例多次从数据库获取记录,总是相同的结果
- ios - 如何更改 MGLPolyline 的颜色?
- php - 从 PHP 向套接字发送数组字节
- javascript - Vue Cli 3. 地理位置。我无法更新用户在 firebase 数据库中的位置