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

Aaron Meurer asmeurer@gmail....
Mon Feb 25 03:45:38 CST 2013


Am I doing something wrong here? I don't have autocall enabled,
because it's too much magic for me, but I wanted to define a special
case of it for print in Python 3.  I figured just defining a magic
called "print" would do the trick, since magics are automatically
called without the %, but it doesn't seem to work:

In [10]: @register_line_magic
def print(line):
    __builtin__.print(line)
   ....:

In [11]: %print 1
1

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

But you can see that other magics work just fine without the %:

In [13]: timeit 1
100000000 loops, best of 3: 17 ns per loop

Also, any tips on how to define this automatically? I tried adding a
file to my startup directory with

from __future__ import print_function

from IPython.core.magic import register_line_magic

@register_line_magic
def print(line):
    __builtin__.print(line)

but it gives

Error in generated code:
def print(line):
    return _call_(_func_, line)

  File "<string>", line 1
    def print(line):
            ^
SyntaxError: invalid syntax

I guess that's a bug? I guess I could add a conditional on the Python version.

Aaron Meurer


More information about the IPython-User mailing list