IPython表示类 [英] IPython representation of classes
问题描述
我正在尝试使用我创建的模块进行IPython,并且它没有显示类对象的实际表示。相反它显示类似
I was trying IPython with a module I created and it does not show the actual representation of class objects. Instead it shows something like
TheClass.__module__ + '.' + TheClass.__name__
我严重在此模块中使用元类,我有真正有意义的类表示,应该向用户显示。
I heavily use metaclasses in this module and I have really meaningful class representations that should be shown to the user.
是否有一个特定于IPython的方法我可以更改以使正确的表示形式可用而不是此命名空间,这在此应用程序中是无用的?
Is there an IPython specific method I can change to make the right representation available instead of this namespace thingy that is quite useless in this application?
或者,如果不可能,我如何自定义我的IPython版本以显示我想要的信息?
Or, if that's not possible, how can I customize my version of IPython to show the information I want?
编辑
作为补充信息,如果我得到一个类并将 __ module __
属性更改为例如没有,当试图显示表示时,它会受到此追溯的影响:
As complementary information, if I get a class and change the __module__
attribute to e.g. None, it blows with this traceback when trying to show the representation:
Traceback (most recent call last):
... [Huge traceback] ...
File "C:\Python32\lib\site-packages\IPython\lib\pretty.py", line 599, in _type_pprint
name = obj.__module__ + '.' + obj.__name__
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
所以我的期望是正确的,这个函数用于显示类对象:
So my expectations were right and this function is used to show class objects:
def _type_pprint(obj, p, cycle):
我试着在课堂上自定义它但我不认为我做得对。这个模块 IPython.lib.pretty
确实有一个大字典链接类型
(元类的父元素)和这个函数。
I tried customizing it in my class but I don't think I'm doing it right. This module IPython.lib.pretty
does have a big dictionary linking type
(the parent of metaclasses) with this function.
编辑2
我尝试过的事情:
- 将
_repr_pretty _
函数添加到元类中。它适用于实例但不适用于类... - 使用此函数
IPython.lib.pretty
.for_type(typ,func)
。它只更改了上面写的大字典,但没有更改RepresentationPrinter
实例制作的副本...所以这个函数完全没用了!! - 调用魔术函数
%pprint
。它使用所有对象的默认Python__ repr __
禁用(或启用)这个漂亮的打印功能。这很糟糕,因为列表,字典和其他许多内容的漂亮印刷非常好。
- Adding the
_repr_pretty_
function to metaclass. It do work with instances but not with classes... - Using this function
IPython.lib.pretty
.for_type(typ, func)
. It only changes the big dictionary a wrote above but not the copy of it made by theRepresentationPrinter
instance... So this function has no use at all?! - Calling the magic function
%pprint
. It disables (or enables) this pretty print feature, using the default Python__repr__
for all the objects. That's bad because the pretty printing of lists, dict and many others are quite nice.
第一种方法更多是我想要的东西,因为它不会影响环境并且特定于此类。
The first approach is more of what I want because it does not affect the environment and is specific for this class.
推荐答案
这只是问题。现在可以这样做:
This is just an issue with IPython 0.12 and older versions. Now is possible to do:
class A(type):
def _repr_pretty_(cls, p, cycle):
p.text(repr(self))
def __repr__(cls):
return 'This Information'
class B: #or for Py3K: class B(metaclass=A):
__metaclass__ = A
它会显示 B
的所需表示。
这篇关于IPython表示类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!