[Python-ideas] sys.py3k

Nick Coghlan ncoghlan at gmail.com
Mon Nov 5 09:04:41 CET 2012


On Mon, Nov 5, 2012 at 4:55 PM, Georg Brandl <g.brandl at gmx.net> wrote:
> Am 04.11.2012 22:49, schrieb anatoly techtonik:
>> if sys.py3k:
>>   # some py2k specific code
>>   pass
>>
>>
>> Why?
>>  1. readable
>>  2. traceable
>>
>> Explained:
>>  1. self-explanatory
>>  2. sys.version_info >= (3, 0) or  sys.version[0] == '3' is harder to
>> trace when you need to find all python 3 related hacks
>
> This proposal is roughly 3 minor versions late.  I can offer you
> a sys.py3_4 attribute though... ;)

Even better (http://packages.python.org/six/#package-contents):

    import six

    if six.PY3:
        # Ooh, Python 3
    else:
        # Not Python 3

If anyone is trying to do single code base Python 2/3 support without
relying on six, they're doing it wrong. Even bundling a copy (if you
don't want to deal with dependency management issues) is a better idea
than reinventing that wheel.

If you *are* rolling your own (or need additional compatibility fixes
that six doesn't provide), then all Python 2/3 compatibility hacks
should be located in a small number of compatibility modules. They
*shouldn't* be distributed widely throughout your codebase.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list