Question: Writing python2 codes in python3

Question

Writing python2 codes in python3

Answers 1
Added at 2017-10-05 09:10
Tags
Question

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?

Answers to

Writing python2 codes in python3

nr: #1 dodano: 2017-10-05 19:10
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.

Source Show
◀ Wstecz