告诉IPython使用对象的`__str __而不是`__repr__`来输出 [英] Tell IPython to use an object's `__str__` instead of `__repr__` for output
问题描述
默认情况下,当IPython显示一个对象时,它似乎使用 __ repr __
。
By default, when IPython displays an object, it seems to use __repr__
.
__ repr __
应该生成一个唯一的字符串,在给定合适的环境的情况下,该字符串可用于重建对象。
这与 __ str __
不同,它应该产生人类可读的输出。
__repr__
is supposed to produce a unique string which could be used to reconstruct an object, given the right environment.
This is distinct from __str__
, which supposed to produce human-readable output.
现在假设我们已经编写了一个特定的类,我们希望IPython默认生成人类可读的输出(即没有显式调用 print
或 __ str __
)。
我们不想通过让我们班级的 __ repr __
做 __ str __
的工作来捏造它。
那会破坏规则。
Now suppose we've written a particular class and we'd like IPython to produce human readable output by default (i.e. without explicitly calling print
or __str__
).
We don't want to fudge it by making our class's __repr__
do __str__
's job.
That would be breaking the rules.
有没有办法告诉IPython调用 __ str __
by特定类的默认值?
Is there a way to tell IPython to invoke __str__
by default for a particular class?
推荐答案
这当然是可能的;你只需要实现实例方法 _repr_pretty_(self)
。 IPython.lib.pretty
。它的实现可能如下所示:
This is certainly possible; you just need implement the instance method _repr_pretty_(self)
. This is described in the documentation for IPython.lib.pretty
. Its implementation could look something like this:
class MyObject:
def _repr_pretty_(self, p, cycle):
p.text(str(self) if not cycle else '...')
p
参数是 IPython.lib.pretty.PrettyPrinter
,您应该使用哪些方法输出文本您正在格式化的对象的表示形式。通常你会使用 p.text(text)
,只是将给定的 text
逐字添加到格式化的表示中,但是你如果你的类代表一个集合,可以做一些事情,比如开始和结束组。
The p
parameter is an instance of IPython.lib.pretty.PrettyPrinter
, whose methods you should use to output the text representation of the object you're formatting. Usually you will use p.text(text)
which just adds the given text
verbatim to the formatted representation, but you can do things like starting and ending groups if your class represents a collection.
cycle
参数是一个布尔值指示是否检测到引用循环 - 即,您是否尝试在同一个调用堆栈中对对象进行两次格式化(这会导致无限循环)。根据您使用的对象类型,可能会或可能不需要考虑它,但它不会受到伤害。
The cycle
parameter is a boolean that indicates whether a reference cycle is detected - that is, whether you're trying to format the object twice in the same call stack (which leads to an infinite loop). It may or may not be necessary to consider it depending on what kind of object you're using, but it doesn't hurt.
作为奖励,如果你想为你不可以访问(或者更确切地说,不想)修改的代码的类,或者你只想对测试进行临时更改,可以使用IPython显示格式化程序的 for_type
方法,如此示例自定义 int
显示。在您的情况下,您将使用
As a bonus, if you want to do this for a class whose code you don't have access to (or, more accurately, don't want to) modify, or if you just want to make a temporary change for testing, you can use the IPython display formatter's for_type
method, as shown in this example of customizing int
display. In your case, you would use
get_ipython().display_formatter.formatters['text/plain'].for_type(
MyObject,
lambda obj, p, cycle: p.text(str(obj) if not cycle else '...')
)
MyObject
当然代表您要自定义打印的类型。请注意,lambda函数带有与 _repr_pretty _
相同的签名,并且工作方式相同。
with MyObject
of course representing the type you want to customize the printing of. Note that the lambda function carries the same signature as _repr_pretty_
, and works the same way.
这篇关于告诉IPython使用对象的`__str __而不是`__repr__`来输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!