mardi 19 juillet 2016

Enabling `xrange` in Python3 for portability

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?

Aucun commentaire:

Enregistrer un commentaire