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?
six
. – Disinteresteddict.items()
,range()
, ...) and accept that it may perform sub-optimally with Python 2. So far no-one ever complained because most of the time the memory overhead is negligible. – Acutance