[Python-il] Where should Python 2/Py3 compatibility shims be placed?
shlomif at shlomifish.org
Mon Apr 10 10:10:02 IDT 2017
On Sun, 9 Apr 2017 00:18:47 +0300
Shai Berger <shai at platonix.com> wrote:
> Hi Shlomi,
> On Saturday 08 April 2017 21:22:27 Shlomi Fish wrote:
> > if sys.version_info > (3,):
> > long = int
> > xrange = range
> > This is a py2/py3 compatibility shim.
> > My question is whether I should put it only at the main program's file or
> > also at each and every *.py module that I load? I'm not sure I understand
> > the python handling of symbols well enough to answer that.
> Such assignments, if done as you wrote, only affect the file they are in.
> However, there are two ways in which You're Doing It Wrong(TM):
> 1) Rather than writing your own compatibility shims, use six, the de-facto-
> standard compatibility library.
> 2) The specific snippet you wrote indicates that your code is essentially
> Python 2, and you add shims to make it work under Python 3. But Python 2 is
> going away, and the future is Python 3; you should write your programs as
> Python 3 code, with shims to make them run under Python 2. This is also the
> approach that six promotes.
> Of course, shims only go so far, and if you want py2/py3 programs you have no
> choice but to avoid certain py3 features and write the code in a "python 2"
> way. But if you intend your code to live on, then it will be Python 2 support
> which you'll drop at some point; you want that change to be as close as
> possible to "just drop the shims".
Thanks for your insights. I'll take them into consideration.
> Hope this helps,
>  https://pythonhosted.org/six/
> Python-il mailing list
> Python-il at hamakor.org.il
Shlomi Fish http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan
Fluttershy ( http://mlp.wikia.com/wiki/Fluttershy ) immediately stops crying
when Emma Watson comes to visit her.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
More information about the Python-il