7

I wrote a script which I wanted to enable for both Python 2 and Python 3.

After importing division and print_function from __future__, my only concern was that my range returns a whole array in Python 2, wasting time and memory.

I added the following 3 lines at the beginning of the script, as a workaround:

if sys.version_info[0] == 3:
    def xrange(i):
        return range(i)

Then, I only used xrange in my code.

Is there some more elegant way to do it rather than my workaround?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126

1 Answers1

13

You can simplify it a bit:

if sys.version_info[0] == 3:
    xrange = range

I would do it the other way around:

if sys.version_info[0] == 2:
    range = xrange

If you ever want to drop Python 2.x support, you can just remove those two lines without going through all your code.

However, I strongly suggest considering the six library. It is the de-facto standard for enabling Python 2 and 3 compatibility.

from six.moves import range
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • I totally agree that the other way you suggest seems more usable. It's actually making Python2's `range` act like Python3's `range`, instead of intoducing `xrange` to Python3 and leaving Python3's `range` different from Python2's `range` – SomethingSomething Aug 17 '17 at 07:52
  • in python3 the second version gives an error, as there is an "if" statement which is not executed, but then `range` variable is considered undefined. This is frustrating. I switched to `six` but it would be actually more preferable to avoid using an additional package. – Sergey Dovgal May 31 '18 at 11:37
  • @SergeyDovgal I just verified that the second version works as expected in Python 3.6.3. Or do you mean that you get an error in your IDE? – Daniel Hepper Jun 04 '18 at 12:36
  • @DanielHepper You are right, I just tried it in an interpreter and it works. However, I recall that the error obtained was related to code launched after installation script. Essentially I got "UnboundLocalError: local variable referenced before assignment", but I need to explain how to reproduce it. I will recall and describe a bit after. – Sergey Dovgal Jun 04 '18 at 16:15