Welcome, guest | Sign In | My Account | Store | Cart

I use this small code close to the head of section of my codes if I'm going to use range function to make it behave same in both python 3 and python 2

Python, 4 lines
1
2
3
4
from sys import version_info as version

if version.major == 2:
    range = xrange

1 comment

Yaşar Arabacı (author) 10 years, 6 months ago  # | flag

It seems like this is causing problems with python earlier then 2.7, It is better to use it like this:

from sys import version_info
if version_info[0] == 2:
    range = xrange
Created by Yaşar Arabacı on Sun, 24 Jul 2011 (MIT)
Python recipes (4591)
Yaşar Arabacı's recipes (3)

Required Modules

  • (none specified)

Other Information and Tasks