Re: print representation for Cython methods in IPython 0.13

Skip to first unread message

Bradley Froehle

Oct 26, 2012, 12:59:35 PM10/26/12
Hi John:

There's nothing to be done in Cython here.  As you point out, a.seed is a builtin (i.e. compiled) method and type(a.seed) returns `builtin_function_or_method` correctly.

This behavior is consistent with builtin Python objects.

In [24]: f = open('links.txt', 'r')

In [25]:
Out[25]: <function read>

In [26]: type(
Out[26]: builtin_function_or_method

In [27]: print
<built-in method read of file object at 0x25830c0>

Can you file a bug report with IPython?


On Friday, October 26, 2012 8:50:47 AM UTC-7, John Palmieri wrote:
This is essentially quoted from a post to the IPython mailing list:

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,

gets applied, rather than this line:

Here is an example from numpy illustrating this, and there are also examples in Sage (like 4.gcd -- see

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>

In [6]: type(a.seed)
Out[6]: builtin_function_or_method

In [7]: a.seed.__class__.__module__
Out[7]: '__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? We could patch IPython, but maybe a better solution would be to change Cython so that its methods are not built in functions. Suggestions?



Reply all
Reply to author
0 new messages