返回 ipython 中对象的实例时使用了哪些属性?
What attributes are used when returning an instance of the object in ipython?
在 ipython 终端中,假设我创建了一个对象,然后只需键入该对象的名称并点击 return,该对象的 attributes/methods 是什么(以什么顺序)查询以生成 return 显示到屏幕的输出?
例如,
In [1]: from mymodule import myclass
In [2]: C = myclass()
In [3]: C # hit return
Out[3]:
查询 C 的哪些 attributes/methods 以生成 Out[3]
中的输出?
更新:从答案(以及我发现的,它表明调用了__repr__
。但是,我有一个class 定义了 __repr__
,但它似乎没有被使用,我收到以下回溯错误:
/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in __call__(self, result)
244 self.start_displayhook()
245 self.write_output_prompt()
--> 246 format_dict, md_dict = self.compute_format_data(result)
247 self.update_user_ns(result)
248 self.fill_exec_result(result)
/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
148
149 """
--> 150 return self.shell.display_formatter.format(result)
151
152 # This can be set to True by the write_output_prompt method in a subclass
/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
150 return {}, {}
151
--> 152 format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude)
153
154 if format_dict or md_dict:
<decorator-gen-12> in __call__(self, obj, include, exclude)
/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in catch_format_error(method, self, *args, **kwargs)
215 """show traceback on failed format call"""
216 try:
--> 217 r = method(self, *args, **kwargs)
218 except NotImplementedError:
219 # don't warn on NotImplementedErrors
/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in __call__(self, obj, include, exclude)
962 return printer(obj)
963 # Finally look for special method names
--> 964 method = get_real_method(obj, self.print_method)
965
966 if method is not None:
/usr/local/lib/python2.7/dist-packages/IPython/utils/dir2.pyc in get_real_method(obj, name)
63
64 try:
---> 65 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
66 except Exception:
67 return None
最终它尝试使用 __getattr__
方法!在我的 class 中,我有自己定义的 __getattr__
方法,这会导致问题吗?
作为另一个答案,例如 的答案一般说明 __repr__
方法将被调用。
但是,ipython 会搜索 __repr__
方法的其他特殊版本以在格式化输出时使用,例如,请参阅 [=31= 的 format
方法的注释]的DisplayFormatter
class:
If an object implement `_repr_mimebundle_` as well as various
`_repr_*_`, the data returned by `_repr_mimebundle_` will take
precedence and the corresponding `_repr_*_` for this mimetype will
not be called.
因此,格式化程序函数使用 get_real_method()
函数检查是否存在特定方法(参见 here), which calls getattr
using a dummy method name that should not exist. In my case I had defined my class to have its own __getattr__
method (and this is where my problems arose), which used the hasattr
function. Due to issues with hasattr
discussed here 这会导致问题,在我的情况下,这意味着卡在 __getattr__
函数中。因此,如果您定义自己的 __getattr__
函数,请务必小心,因为它可能会产生意想不到的后果!
在 ipython 终端中,假设我创建了一个对象,然后只需键入该对象的名称并点击 return,该对象的 attributes/methods 是什么(以什么顺序)查询以生成 return 显示到屏幕的输出?
例如,
In [1]: from mymodule import myclass
In [2]: C = myclass()
In [3]: C # hit return
Out[3]:
查询 C 的哪些 attributes/methods 以生成 Out[3]
中的输出?
更新:从答案(以及我发现的__repr__
。但是,我有一个class 定义了 __repr__
,但它似乎没有被使用,我收到以下回溯错误:
/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in __call__(self, result)
244 self.start_displayhook()
245 self.write_output_prompt()
--> 246 format_dict, md_dict = self.compute_format_data(result)
247 self.update_user_ns(result)
248 self.fill_exec_result(result)
/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
148
149 """
--> 150 return self.shell.display_formatter.format(result)
151
152 # This can be set to True by the write_output_prompt method in a subclass
/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
150 return {}, {}
151
--> 152 format_dict, md_dict = self.mimebundle_formatter(obj, include=include, exclude=exclude)
153
154 if format_dict or md_dict:
<decorator-gen-12> in __call__(self, obj, include, exclude)
/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in catch_format_error(method, self, *args, **kwargs)
215 """show traceback on failed format call"""
216 try:
--> 217 r = method(self, *args, **kwargs)
218 except NotImplementedError:
219 # don't warn on NotImplementedErrors
/usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc in __call__(self, obj, include, exclude)
962 return printer(obj)
963 # Finally look for special method names
--> 964 method = get_real_method(obj, self.print_method)
965
966 if method is not None:
/usr/local/lib/python2.7/dist-packages/IPython/utils/dir2.pyc in get_real_method(obj, name)
63
64 try:
---> 65 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
66 except Exception:
67 return None
最终它尝试使用 __getattr__
方法!在我的 class 中,我有自己定义的 __getattr__
方法,这会导致问题吗?
作为另一个答案,例如__repr__
方法将被调用。
但是,ipython 会搜索 __repr__
方法的其他特殊版本以在格式化输出时使用,例如,请参阅 [=31= 的 format
方法的注释]的DisplayFormatter
class:
If an object implement `_repr_mimebundle_` as well as various
`_repr_*_`, the data returned by `_repr_mimebundle_` will take
precedence and the corresponding `_repr_*_` for this mimetype will
not be called.
因此,格式化程序函数使用 get_real_method()
函数检查是否存在特定方法(参见 here), which calls getattr
using a dummy method name that should not exist. In my case I had defined my class to have its own __getattr__
method (and this is where my problems arose), which used the hasattr
function. Due to issues with hasattr
discussed here 这会导致问题,在我的情况下,这意味着卡在 __getattr__
函数中。因此,如果您定义自己的 __getattr__
函数,请务必小心,因为它可能会产生意想不到的后果!