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/
_______________________________________________
Python-il mailing list
[email protected]
http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il

לענות