Hello Is there any Python 3.5+ code guidelines for Python 4

English   |   원문

tl;dr

  1. Write 3 only
  2. It will be broken at 2.7
  3. then Duct-taping it

STOP WRITING PYTHON 4 INCOMPATIBLE CODE

Just before I need to write some python project, I saw this at Hacker News. It shows the reason why shouldn't use Six library. But I couldn't find guidelines for Python 4. So I started to write it.

Six: Python 2 and 3 Compatibility Library

Six provides simple utilities for wrapping over differences between Python 2 and Python 3. It is intended to support codebases that work on both Python 2 and 3 without modification. six consists of only one Python file, so it is painless to copy into a project.
The name, “six”, comes from the fact that 2*3 equals 6.

Now, we need TwentyFour.

Really? why not Twelve?

I hope that there are some people who wanna throw Python 2 away. (me too) In my case, particular meaningful libraries block this. (such as Twisted).

So, check first

Some helpful sites for finding a reason to use Python 2.

I don't always use Python 2, but when I do, only 2.7.11+

Python 4?

So, code for Python 3.5+ first

Break it at Python 2.7.11+

Fix it with

  • flake8
  • py.test
  • tox
  • travis CI

Then, What differences exist between Python 3.5+ and Python 2.7.11+?

For 3 users, just follow those rules, then It will be okay:

  • Manage Python Versions with pyenv
  • Manage Python Project Environments with pyenv-virtualenv
  • Shebang: #!/usr/bin/env python
  • print() as a function.
  • u'Everything' and b''
  • class Base(object): (object)
  • except Exception as e: (as)
  • 2/3.0 (think your C/C++ experiences)
  • Dictionary Iteration: Use .items(), .keys(), .values()
  • String Manipulation: Use .format()
  • Typing (3.5+): Comment it
  • Use latest 3rd-party packages
  • Python 2 only requirements.txt?!

When additional problems happened to you, check those:

More links
DISCLAIMER:

Maybe it's not, but I'll update those asap. So, please ping to me (twt@angryonhim). Thanks.