在ipython中返回对象实例时使用哪些属性?

在ipython中返回对象实例时使用哪些属性?,ipython,Ipython,在ipython终端中,假设我创建了一个对象,然后只需键入该对象的名称并点击return,查询该对象的哪些属性/方法(以及以何种顺序)以生成返回到屏幕的输出 例如: 查询C的哪些属性/方法以在Out[3]中生成输出 更新:从答案(以及我找到的a)中,它显示调用了\uuuuuRepr\uuuuUn/code>。但是,我有一个定义\uuuRepr\uUn/code>的类,但它似乎没有被使用,并且我得到以下回溯错误: /usr/local/lib/python2.7/dist-packages/IPy

在ipython终端中,假设我创建了一个对象,然后只需键入该对象的名称并点击return,查询该对象的哪些属性/方法(以及以何种顺序)以生成返回到屏幕的输出

例如:

查询C的哪些属性/方法以在
Out[3]
中生成输出

更新:从答案(以及我找到的a)中,它显示调用了
\uuuuuRepr\uuuuUn/code>。但是,我有一个定义
\uuuRepr\uUn/code>的类,但它似乎没有被使用,并且我得到以下回溯错误:

/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
/usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc在调用中(self,result)
244 self.start_displayhook()
245 self.write_output_prompt()
-->246格式dict,md dict=self.compute格式数据(结果)
247.自我更新用户(结果)
248自我填写执行结果(结果)
/计算格式数据中的usr/local/lib/python2.7/dist-packages/IPython/core/displayhook.pyc(self,result)
148
149         """
-->150返回self.shell.display\u formatter.format(结果)
151
152#这可以通过子类中的write_output_prompt方法设置为True
/格式为usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc(self、obj、include、exclude)
150返回{},{}
151
-->152格式dict,md dict=self.mimebundle\u格式化程序(obj,include=include,exclude=exclude)
153
154如果格式为dict或md dict:
在调用中(self、obj、include、exclude)
/catch_format_错误中的usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc(方法,self,*args,**kwargs)
215“在失败的格式调用上显示回溯”
216尝试:
-->217 r=方法(自,*args,**kwargs)
218除未实施错误外:
219#不要对未实施的错误发出警告
/调用中的usr/local/lib/python2.7/dist-packages/IPython/core/formatters.pyc(self、obj、include、exclude)
962返回打印机(obj)
963#最后寻找特殊的方法名称
-->964方法=获取真实方法(obj,self.print方法)
965
966如果方法不是无:
/get_real_方法(obj,name)中的usr/local/lib/python2.7/dist-packages/IPython/utils/dir2.pyc
63
64尝试:
--->65金丝雀=getattr(对象,“Ipyton”金丝雀方法“不应”存在“无”)
66例外情况除外:
67无返回

当它最终尝试使用
\uuuuuu getattr\uuuuu
方法时!在我的类中,我有自己定义的
\uuuuuuu getattr\uuuuu
方法,这可能会导致问题吗?

作为另一个答案,例如,的答案通常会声明调用
\uu repr\uuu
方法

但是,ipython会搜索格式化输出时要使用的
\uuu repr\uuuu
方法的其他特殊版本,例如,请参阅ipython类的
格式化方法的注释:

因此,格式化程序函数使用
get\u real\u method()
函数(请参阅)检查特定方法是否存在,该函数使用不应该存在的伪方法名调用
getattr
。在我的例子中,我已将我的类定义为有自己的
\uu getattr\uuu
方法(这就是我出现问题的地方),它使用了
hasattr
函数。由于所讨论的
hasattr
问题,这会导致问题,在我的例子中,这意味着陷入
\uuuu getattr\uuuuuu
函数中。因此,如果您定义自己的
\uu getattr\uuuuuu
函数,道德上要非常小心,因为它可能会产生意外的后果

/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
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.