[IPython-dev] Cython method printers

Jason Grout jason-sage at creativetrax.com
Tue Oct 23 15:55:02 EDT 2012


We're running into a printing difference with Cython methods in the new 
IPython.  I think the problem boils down to: methods of Cython objects 
are classed as "built in functions", even though their repr reveals more 
information.  So for Cython methods, 
https://github.com/ipython/ipython/blob/master/IPython/lib/pretty.py#L669 gets 
applied, rather than this line: 
https://github.com/ipython/ipython/blob/master/IPython/lib/pretty.py#L671.

Here is an example from numpy illustrating this:

In [1]: import numpy as np

In [2]: a=np.random.mtrand.RandomState()

In [3]: a.seed
Out[3]: <function seed>

In [4]: repr(a.seed)
Out[4]: '<built-in method seed of mtrand.RandomState object at 0x1004d93c0>'

In [5]: print a.seed
<built-in method seed of mtrand.RandomState object at 0x1004d93c0>

[snip]

In [8]: a.seed.__class__.__module__
Out[8]: '__builtin__'


Notice that str() and repr() of the Cython method gave much more useful 
information than the default built-in function printer.

How do we fix this?  I guess we could make the built-in function printer 
check the repr to see if more information than just <built-in function 
name> is available, but that seems a bit hackish.

Thanks,

Jason



More information about the IPython-dev mailing list