Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IPython忽略对象';打印时,请重复_Python_Ipython - Fatal编程技术网

IPython忽略对象';打印时,请重复

IPython忽略对象';打印时,请重复,python,ipython,Python,Ipython,我已经编写了一个子类defaultdict,我给了它自己的\uuuu repr\uuu方法,以便在交互式会话中自定义它的外观 在常规Python会话中,这将按预期工作: Python 3.5.0 (default, Sep 20 2015, 11:28:25) [GCC 5.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from collecti

我已经编写了一个子类
defaultdict
,我给了它自己的
\uuuu repr\uuu
方法,以便在交互式会话中自定义它的外观

在常规Python会话中,这将按预期工作:

Python 3.5.0 (default, Sep 20 2015, 11:28:25) 
[GCC 5.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import defaultdict
>>> class Foo(defaultdict):
...   def __repr__(self):
...     return 'foo_repr'
... 
>>> foo = Foo()
>>> foo
foo_repr
。。。但在IPython中,它没有:

In [1]: from collections import defaultdict

In [2]: class Foo(defaultdict):
   ...:     def __repr__(self):
   ...:         return 'foo_repr'
   ...:     

In [3]: foo = Foo()

In [4]: foo
Out[4]: defaultdict(None, {})

In [5]: repr(foo), str(foo)
Out[5]: ('foo_repr', 'foo_repr')
IPython在这里干什么?我怎么能阻止它

编辑:定义
\u repr\u pretty\u
有助于:

In [1]: from collections import defaultdict

In [2]: class Foo(defaultdict):
    def __repr__(self):
        return 'foo_repr'
    def _repr_pretty_(self, p, cycle):
        p.text('repr_foo_pretty')
   ...:         

In [3]: foo = Foo()

In [4]: foo
Out[4]: repr_foo_pretty

但我还是不太明白发生了什么。显然,
\u repr\u pretty
以前没有在任何基类上定义。IPython是如何使用
defaultdict
\uu repr\uuuu
而不是
Foo
的?为什么它会发生在
defaultdict
而不是其他很多人身上?

可能与此相关,它正在按预期工作,无法在这里看到问题……repr和str都是如此。@jornsharpe谢谢。我更新了问题。