简体   繁体   中英

Writing python2 codes in python3

Can I write python 2 syntaxes in python 3 file and compile with python 3 compiler? What I mean is this:

I know that by using future__import one can use several python 3 functions (like, print , exep ) in python 2.

What I want to know is can I do the opposite of this? Let's say I have a code for printing to data file in python2 ( print>> f,a ). Now I want to use the same code in python3 compiler. Can I do it?

import __future__

Are Python3 builtin modules written using Python 2 grammar/syntax.

They either include functions not available in Python2 or override the Python2 version.

Having a 'forward port' doesn't make a whole lot of sense as Python3 still does all of the same things Python2 does, albeit, in a (arguably) better way.

This is not different than importing any other module; it simply provides methods/classes/etc to operate on.

If you have your own Python2 code you want to use in Python3 your best bet is to run the 2to3 conversion tool .

It's actually not very difficult to use. I have myself converted thousands of scripts using this tool.

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