[IPython-User] print "autocall" in Python 3

Thomas Kluyver takowl@gmail....
Mon Feb 25 05:03:36 CST 2013


On 25 February 2013 09:45, Aaron Meurer <asmeurer@gmail.com> wrote:

> In [12]: print 1
>   File "<ipython-input-12-08890221e919>", line 1
>     print 1
>           ^
> SyntaxError: invalid syntax
>

This does seem like a bug. It's not getting translated into the call with
the magic (use hist -t to see translations). I guess something must be
treating it as Python 2 syntax. File a bug so we don't forget it.

If it helps, you can force automatic brackets using a slash:

In [1]: /print 123 456
123 456



> Error in generated code:
> def print(line):
>     return _call_(_func_, line)
>
>   File "<string>", line 1
>     def print(line):
>             ^
> SyntaxError: invalid syntax
>

(Just tested, this error is only when running in Python 2, yes?)

This is because of the decorator library we're using. In order to preserve
the function signature, it actually assembles a new function as a string,
and calls compile() on it. Of course, in that context,
__future__.print_function is not in effect, so you can't redefine print.

Short of dropping the decorator library, I don't think there's much we can
do about that. Adding a condition to check the Python version first should
work.

Finally, don't forget that you'll need to evaluate the input, otherwise...

In [1]: a = 42

In [2]: %print a
a

Best wishes,
Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20130225/9471b323/attachment-0001.html 


More information about the IPython-User mailing list