简体   繁体   中英

Python Old print statement

I just got Syntax error on 'print' with Python 3, and by searching I found that in the newer python version print statement is replaced with print function ( http://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function ).

So what was 'print' before python 3? And how it was implemented? Is it possible to create statement like old 'print' which act like functions?

As you say, it was a statement. Statements are handled at the interpreter level, because each one has their own rules in the parsing tree; so it's very unlikely that you can add your own statements to the language (without patching the interpreter and compiler code), Python is not designed to be that extensible.

There are still some provisions to make that kind of hacks, you could take a look on the way the from future import ... works:

http://docs.python.org/2/reference/simple_stmts.html#future

If you want to convert python 2 code to python 3, you could use 2to3 script. see http://docs.python.org/2/library/2to3.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM