[GRASS-dev] [GRASS GIS] #3757: Python3: range vs xrange

GRASS GIS trac at osgeo.org
Wed Feb 20 03:12:58 PST 2019


#3757: Python3: range vs xrange
--------------------------+-------------------------
  Reporter:  neteler      |      Owner:  grass-dev@…
      Type:  defect       |     Status:  new
  Priority:  normal       |  Milestone:  7.8.0
 Component:  Python       |    Version:  svn-trunk
Resolution:               |   Keywords:  python3
       CPU:  Unspecified  |   Platform:  Unspecified
--------------------------+-------------------------

Comment (by pmav99):

 Python 2 has both {{{range()}}} and {{{xrange()}}}.
 In Python 3, {{{range()}}} has been removed and {{{xrange()}}} has been
 renamed to {{{range()}}}.
 So any code using {{{xrange()}}} is not Python 3 compatible, so the answer
 is yes {{{xrange()}}} needs to be replaced by {{{range()}}}. This does
 lead to bigger RAM usage  ({{{range()}}} creates a list while
 {{{xrange()}}} creates an iterator, although in the vast majority of the
 cases the impact is negligible (and when it isn't you should probably
 rethink your algorithms).

 The alternative is to introduce a "compatibility layer". E.g.

 {{{
 import sys

 if sys.version_info.major < 3:
     range = xrange
 }}}

 but this needs to be used in every module (or added to a {{{compat.py}}}
 module and imported from any other module. Moreover, this can break
 something (if people were abusing {{{range()}}} that is).

 All that being said, when cross-version compatibility is required it is
 usually a good idea to use either six and/or future. I believe six is
 already being pulled by GRASS dependencies (matptlotlib if memory serves).

 https://pypi.org/project/six/
 https://pypi.org/project/future/

-- 
Ticket URL: <https://trac.osgeo.org/grass/ticket/3757#comment:1>
GRASS GIS <https://grass.osgeo.org>



More information about the grass-dev mailing list