set variable to "print" in python 2 -


i want have alias print statement. following method fails syntaxerror:

>>> = print 

how can i, if @ all, set new alias print statement? python version -> 2.7

normally, can't shadow python keywords (which used make python "statements" such print, for, exec, if, ...). however, print special. ease compatibility python3.x, can drop print statement using

from __future__ import print_function 

at top of module (before other imports other non-__future__ imports).

this disables print statement , unshadows print function (which can shadow once more whatever user defined function want). e.g. (from repl):

python 2.7.10 (default, oct 23 2015, 18:05:06)  [gcc 4.2.1 compatible apple llvm 7.0.0 (clang-700.0.59.5)] on darwin type "help", "copyright", "credits" or "license" more information. >>> __future__ import print_function >>> def foo(): pass ...  >>> print = foo >>>  >>> print('foo') traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: foo() takes no arguments (1 given) 

with said, think aliasing builtin functions bad idea. future readers of code (including yourself) confused when try use code , print doesn't conditioned believe. remember zen of python (import this):

...

readability counts.

...


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -