2

I know python 2to3 changes all xrange to range and I don't see any problem with this. My question is about how it changes range(...) into list(range(...)): is it dumb and just blindly search and replaces them all, or is it smart enough to tell when the list isn't needed and leaving it as range would be better?

wim
  • 338,267
  • 99
  • 616
  • 750

1 Answers1

3

I don't know how intelligent it really is, but it certainly doesn't add list() to every range().

For example, the following:

print(range(10))

is changed to:

print(list(range(10)))

However, the following:

for el in range(10):
  print(el)

is left untouched.

This clearly indicates that it's more sophisticated than a blind search-and-replace.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I noticed that if I rebind the name range to something else, and then use it later in the script, it still munges it. So it can't be very intelligent .. – wim Mar 13 '13 at 11:51
  • it also broke my doctests, because it ignored the xranges in those :P – wim Mar 13 '13 at 12:25