Hello Is there any Python 3.5+ code guidelines for Python 4
tl;dr
- Write 3 only
- It will be broken at 2.7
- 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?
-
Why Python 4.0 won't be like Python 3.0
My current expectation is that Python 4.0 will merely be "the release that comes after Python 3.9". That's it.
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+?
- New features: https://docs.python.org/dev/whatsnew/index.html
- 3.5: typing, ...
- 3.4: asyncio, enum, ...
- 3.3: u'unicode' (again), venv, ...
- 3.2: concurrent.futures, PEP-3333, ...
- The key differences between Python 2.7.x and Python 3.x with examples
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'
andb''
-
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?!
- Future
- Enum (3.4+): enum34 at 2.7
- what about requirements.txt?! do we divide it?
When additional problems happened to you, check those:
- Porting Python 2 Code to Python 3
- Cheat Sheet: Writing Python 2-3 compatible code
- Using Python 2-only dependencies on Python 3
- Nine: Python 2 / 3 compatibility, like six, but favouring Python 3
More links
DISCLAIMER:
Maybe it's not, but I'll update those asap. So, please ping to me (twt@angryonhim). Thanks.