title: Is "backporting" Python 3's `range` to Python 2 a bad idea?
body: <p>One of my classes requires assignments to be completed in Python, and as an exercise, I've been making sure my programs work in both Python 2 and Python 3, using a script like this:</p>
  
  <pre><code>#!/bin/bash
  # Run some PyUnit tests
  python2 test.py
  python3 test.py
  </code></pre>
  
  <p>One thing I've been doing is making <code>range</code> work the same in both versions with this piece of code:</p>
  
  <pre><code>import sys
  
  # Backport Python 3's range to Python 2 so that this program will run
  # identically in both versions.
  if sys.version_info &lt; (3, 0):
      range = xrange
  </code></pre>
  
  <p>Is this a bad idea?</p>
  
  <p>EDIT:</p>
  
  <p>The reason for this is that <code>xrange</code> and <code>range</code> work differently in Python 2 and Python 3, and I want my code to do the same thing in both. I could do it the other way around, but making Python 3 work like Python 2 seems stupid, since Python 3 is "the future".</p>
  
  <p>Here's an example of why just using <code>range</code> isn't good enough:</p>
  
  <pre><code>for i in range(1000000000):
      do_something_with(i)
  </code></pre>
  
  <p>I'm obviously not using the list, but in Python 2, this will use an insane amount of memory.</p>