six.moves.builtins.range is not consistent in Python 2 and Python 3

北慕城南 提交于 2019-12-04 13:08:17

I believe you just want six.moves.range. Not, six.moves.builtins.range.

>>> # tested on python2.x..
>>> import six.moves as sm
>>> sm.range
<type 'xrange'>

The reason here is that six.moves.builtins is the version agnostic "builtins" module. That just gives you access to the builtins -- it doesn't actually change what any of the builtins are.

Typically, I don't feel the need to introduce the external dependency in cases like this. I usually just add something like this to the top of my source file:

try:
    xrange
except NameError:  # python3
    xrange = range
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!