de la importación anterior print_statement
Frecuentes
Visto 2,796 veces
15
¿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.
2 Respuestas
2
Some suggestion for IPython
%autocall
print "Hi"
Define magic with autocall on
from IPython.core.magic import register_line_magic
@register_line_magic
def p(line):
print(line)
p "Hi"
Define magic with autocall off
from IPython.core.magic import register_line_magic
@register_line_magic
def p(line):
print(eval(line))
%p "Again"
You could create a .config/ipython/profile_default/startup/autoprint.py file for you line magic functions.
respondido 27 nov., 13:19
0
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'
f()
huellas dactilares foo
.
Edit:
IPython also has an
%autocall
magic function that, well, automatically calls functions, so after doing%autocall 1
, mecanografíaprint 3
in a cell acts likeprint
in python 2.x (though this unfortunately doesn't work in function definitions like/
hace.Also, since it's a function and not a statement, you can simply do
p = print
, so later/p 'foo'
calls give you almostpdb
-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 python python-3.x printing or haz tu propia pregunta.
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. - tobias_kYou may be looking for Ruby - Ryan Haining
Any downvoters care to suggest question improvements? - beardc
@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. - beardcIf 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. - tobias_k