de la importación anterior print_statement

¿Hay algún equivalente a from __future__ import print_function that forward-ports the print statement from python 2.x?

An answer involving some ipython magic that lets me print without need of surrounding parens during prototyping is also acceptable.

preguntado el 27 de noviembre de 13 a las 18:11

If it's just for the interactive shell, you do not need to print at all, do you? Just write an expression and you see the result. -

You may be looking for Ruby -

Any downvoters care to suggest question improvements? -

@jazzpi Not a particularly good programming reason... I appreciate the print function's improvements, but I try to reduce # of unnecessary keystrokes for health reasons. Difference between adding the statement/function to an existing line is an extra Shift-( Ctrl-e Shift-)=6 keystrokes for me. I'd grep-change it when I'm done with the program but it makes it easier while developing, especially since I end up deleting most print statements by the end anyways. -

If it's just to reduce typing and strain on wrists, you could also look for a editor / interactive shell that automatically inserts () after function names. -

2 Respuestas

Some suggestion for IPython

print "Hi"

Define magic with autocall on

from IPython.core.magic import register_line_magic
def p(line):
p "Hi"

Define magic with autocall off

from IPython.core.magic import register_line_magic
def p(line):
%p "Again"

You could create a .config/ipython/profile_default/startup/ file for you line magic functions.

respondido 27 nov., 13:19

Provisional answer (A solution that doesn't involve a leading / will be preferred.): IPython llama automáticamente an object if line begins with a /, even if it's indented:

def f():
    /print 'foo'


huellas dactilares foo.


  1. IPython also has an %autocall magic function that, well, automatically calls functions, so after doing %autocall 1, mecanografía print 3 in a cell acts like print in python 2.x (though this unfortunately doesn't work in function definitions like / hace.

  2. Also, since it's a function and not a statement, you can simply do p = print, so later /p 'foo' calls give you almost pdb-like behavior (something I was going for but that wasn't explicitly stated in the question).

respondido 27 nov., 13:19

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.