[IPython-user] Ipython print function problems

Fernando Perez fperez.net@gmail....
Mon Aug 3 17:10:06 CDT 2009


On Fri, Jul 31, 2009 at 1:15 PM, Dr. Phillip M.
Feldman<pfeldman@verizon.net> wrote:
>
> How about the following?
>
> print str(x) + '+' + str(y) + '=' + str(x+y)
> 1+2=3
>
>
> DeR13 wrote:
>>
>> Hi everyone,
>>
>> I am a complete noob at Ipython and have run across a problem.
>> If I type a simple code into the regular python terminal I get this
>> output:
>>
>>>>> x=1
>>>>> y=2
>>>>> print x, "+", y, "=", x+y
>> 1 + 2 = 3
>>
>> However in Ipython the same input yields:
>> In [1]: x=1
>>
>> In [2]: y=2
>>
>> In [3]: print x, "+", y, "=", x+y
>> ------> print(x, "+", y, "=", x+y)
>> (1, '+', 2, '=', 3)
>>
>> Is it possible to configure Ipython to output without the syntax?
>>  1 + 2 = 3 instead of (1, '+', 2, '=', 3)
>>
>> Thanks D

I can't find the original post on this, but I think I can help a little.

This is a consequence of the new 'print as a function' which is
available in Python 2.6, and can be a little surprising.

The behavior that's bothering you is:


In [3]: print x, "+", y, "=", x+y
------> print(x, "+", y, "=", x+y)
(1, '+', 2, '=', 3)

That's because ipython  finds print as a function and automatically
calls it.  You can't directly delete the print function:

In [4]: del print
------------------------------------------------------------
   File "<ipython console>", line 1
     del print
             ^
SyntaxError: invalid syntax


But you can do it with a little workaround:

In [5]: import __builtin__

In [6]: del __builtin__.__dict__['print']

In [7]: print x, "+", y, "=", x+y
1 + 2 = 3


Alternatively, you can turn autocalling off:

In [4]: autocall 0
Automatic calling is: OFF

In [5]: print x, "+", y, "=", x+y
1 + 2 = 3


But then you lose  autocalling for everything.

Which solution  you prefer is up to you.

Cheers,

f


More information about the IPython-user mailing list