[Python-il] Where should Python 2/Py3 compatibility shims be placed?

4 views
Skip to first unread message

Shlomi Fish

unread,
Apr 8, 2017, 2:22:37 PM4/8/17
to pyth...@hamakor.org.il, Shlomi Fish
Hi all!

I sometimes put (per a hint I found on a web search) code like that at the
beginning of my Python programs:

if sys.version_info > (3,):
long = int
xrange = range

(e.g:
https://github.com/shlomif/project-euler/blob/master/project-euler/581/euler_581_v3.py#L4
)

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.

Thanks and happy holidays!

Shlomi Fish
_______________________________________________
Python-il mailing list
Pyth...@hamakor.org.il
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il

Shai Berger

unread,
Apr 8, 2017, 5:19:01 PM4/8/17
to pyth...@hamakor.org.il
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[1], 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".

Hope this helps,
Shai.

[1] https://pythonhosted.org/six/

Shlomi Fish

unread,
Apr 10, 2017, 3:15:12 AM4/10/17
to Shai Berger, pyth...@hamakor.org.il
Hi Shai,

Thanks for your insights. I'll take them into consideration.

Regards,

Shlomi Fish



> Hope this helps,
> Shai.
>
> [1] https://pythonhosted.org/six/
> _______________________________________________
> Python-il mailing list
> Pyth...@hamakor.org.il
> http://hamakor.org.il/cgi-bin/mailman/listinfo/python-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.
http://www.shlomifish.org/humour/bits/facts/Emma-Watson/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Reply all
Reply to author
Forward
0 new messages