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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top