Neat way to port nonpolyglot code from Py2 to Py3

前端 未结 3 2057
抹茶落季
抹茶落季 2021-01-22 10:28

In many of my python modules, I am using

from itertools import izip_longest

But now I am moving my codebase to Py3 (compatible with Py2 at the sa

3条回答
  •  离开以前
    2021-01-22 10:30

    Making python2 code work with python3 is not for free. And there is a lot of grunt work involved. I think it is difficult to avoid this and in any case you have to do proper testing to be sure your code is working with both versions.

    I don't know your code and your project, it's exact contents, whether this project should stay alive for much longer or if you just want to make it survive for a little longer. Thus I'm not sure what's best in your scenario.

    In general I would suggest to change your code such, that it looks like python3 code and still runs with python2 and not to write code that looks like python2 code but also runs with python3. (But all depends on your context)

    You might want to try out the package future https://python-future.org/ Which provides helpers for writing code, that's running with both versions.

    future also contains tools, that try to automatically change code in a way, that it is more likely to run with both versions. Depending on the complexity of your code you still have to do quite some things manually (especially for unicode issues)

    The commands are called futurize (make code look like python3 but run with python2) or pasteurize (make code look like python2 but run also with python3)

    make backups of all your files (or use some version control like git) before trying this out.

    The simple use case is futurize --stage1 yourfile.py

    There is one interesting chapter / cheatsheet with many examples, that is worth reading https://python-future.org/compatible_idioms.html?highlight=zip

    If you do not want to use the future package or if you encounter situations, that future doesn't handle well I would write an adapter module and use that in your code.

    e.g. py2py3compat.py:

    try: 
        # Python 3 
        from itertools import zip_longest
    except ImportError: 
        # Python 2 
        from itertools import izip_longest as zip_longest
    

    yourpyfile.py:

    from py2py3compat import zip_longest
    

    and perform a global search and replace to replace izip_longest with zip_longest

提交回复
热议问题