Let Python be compatible with both Python2 and Python3 's 8 tips for sharing _python

Source: Internet
Author: User

A statement in the Python mailing list said that "Python3 cannot be popularized in 10." It seems to me that this is a bit too pessimistic, python3 and python2 are not compatible, but the difference between them is not as big as many people think. All you need to do is make some changes to your code to support both Python2 and Python3. Here's a quick overview of how to get your Python code to support both Python2 and Python3.

First, give up the Python version before Python 2.6

The python version of Python 2.6 lacks some new features that can cause you a lot of trouble with your migration work. If you are not compelled to give up support for previous versions.

Second, the use of 2TO3 tools for code inspection

2to3 is a python-led code conversion tool that automatically converts python2 code into Python3 code. Of course, unfortunately, the converted code does not deal with Python2 compatibility. So we don't really use 2TO3 to convert the code. Perform 2to3 t.py view output information and fix related issues.

Third, use Python-3 to execute Python program

2to3 can check out a lot of python2&3 compatibility issues, but there are a lot of problems that 2to3 can't find. When the 3 parameter is added, the program will not be able to python2 and Python3 in the console at run time, and the 2TO3 can not handle the problem prompt. For example, Python3 and python2 have changed the processing rules for division. Using the 3 parameter to perform 4/2 will prompt deprecationwarning:classic int division.

Iv. from __future__ Import

The "from __future__ import" allows you to use the future features of Python. The full future feature of Python is visible __future__. All the characters in the Python3 have become Unicode. Unicode characters in python2 need to be defined with U before the character, but no home u in 3, and the program will not be able to compile after adding U. To resolve this problem, you can "from future import unicode_literals" so that the behavior of characters in Python2 will be consistent with Python3, and the definition of normal characters in Python2 will automatically be recognized as Unicode.

V. Import issues

There are a lot of python2 packages in the Python3, and in most cases the packages are changed by name. We can deal with these problems when we import them.

Copy Code code as follows:
Try: #python2
From userdict import userdict
#建议按照python3的名字进行import
From userdict import dictmixin as Mutablemapping
Except Importerror: #python3
From collections Import UserDict
From collections Import mutablemapping

Vi. writing programs using the Python3 method

Python2 print is the keyword, and in the Python3, print becomes a function. In fact, the print function is already in the python2.6, so you can change the instructions in the 2TO3 to the new wording. Some changes have been made to the handling of exceptions in Python3, which is similar to print, and can be modified directly in accordance with the prompts in 2to3.

Check the currently running Python version

Sometimes you may have to write different code for Python2 and Python3, and you can check the Python version of the current system with the following code.

Copy Code code as follows:
Import Sys
If Sys.version > ' 3 ':
PY3 = True
Else
PY3 = False

Eight, six

Six provides a few simple tools to encapsulate the difference between Python 2 and Python 3. I don't recommend using six. If you do not need to support the Python version before python2.6, it is easier to deal with compatibility issues even without six. Using six will make your code more like Python2 than Python3.
The popularity of Python3 needs to be driven by every pythoner, and you may not be able to upgrade immediately to Python3, but start writing compatible Python3 code now and upgrade to Python3 when the conditions are ripe.

Note: The difference between Python2 and Python3

If you have a more comprehensive understanding of the issues associated with migrating from Python2 to Python3, recommend reading porting to Python 3 This is a free Python reading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.