• Python 2.7 range Function provokes a Memory Error

    From Stephen Tucker@21:1/5 to All on Thu Mar 2 11:25:49 2023
    Hi,

    The range function in Python 2.7 (and yes, I know that it is now
    superseded), provokes a Memory Error when asked to deiliver a very long
    list of values.

    I assume that this is because the function produces a list which it then iterates through.

    1. Does the range function in Python 3.x behave the same way?

    2. Is there any equivalent way that behaves more like a for loop (that is, without producing a list)?

    To get round the problem I have written my own software that is used in a
    for loop.

    Stephen Tucker.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Ribbens@21:1/5 to Stephen Tucker on Thu Mar 2 11:29:53 2023
    On 2023-03-02, Stephen Tucker <[email protected]> wrote:
    The range function in Python 2.7 (and yes, I know that it is now
    superseded), provokes a Memory Error when asked to deiliver a very long
    list of values.

    I assume that this is because the function produces a list which it then iterates through.

    1. Does the range function in Python 3.x behave the same way?

    No, in Python 3 it is an iterator which produces the next number in the sequence each time.

    2. Is there any equivalent way that behaves more like a for loop (that is, without producing a list)?

    Yes, 'xrange' in Python 2 behaves like 'range' in Python 3.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to Stephen Tucker on Thu Mar 2 06:34:24 2023
    On 2023-03-02 at 11:25:49 +0000,
    Stephen Tucker <[email protected]> wrote:

    The range function in Python 2.7 (and yes, I know that it is now
    superseded), provokes a Memory Error when asked to deiliver a very long
    list of values.

    I assume that this is because the function produces a list which it then iterates through.

    1. Does the range function in Python 3.x behave the same way?

    No.

    2. Is there any equivalent way that behaves more like a for loop (that is, without producing a list)?

    Try xrange.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Stephen Tucker on Thu Mar 2 22:43:22 2023
    On Thu, 2 Mar 2023 at 22:27, Stephen Tucker <[email protected]> wrote:

    Hi,

    The range function in Python 2.7 (and yes, I know that it is now
    superseded), provokes a Memory Error when asked to deiliver a very long
    list of values.

    I assume that this is because the function produces a list which it then iterates through.

    1. Does the range function in Python 3.x behave the same way?

    No, but list(range(x)) might, for the same reason. In Py2, range
    returns a list, which means it needs a gigantic collection of integer
    objects. In Py3, a range object just defines its start/stop/step, but
    if you call list() on it, you get the same sort of

    2. Is there any equivalent way that behaves more like a for loop (that is, without producing a list)?

    To get round the problem I have written my own software that is used in a
    for loop.

    xrange is an iterator in Py2, so that's the easiest way to handle it.
    Obviously migrating to Py3 would be the best way, but in the meantime,
    xrange will probably do what you need.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stephen Tucker@21:1/5 to [email protected] on Tue Mar 7 05:51:57 2023
    Hi again,

    I tried xrange, but I got an error telling me that my integer was too big
    for a C long.

    Clearly, xrange in Py2 is not capable of dealing with Python (that is,
    possibly very long) integers.

    I am raising this because,

    (a) IF xrange in Py3 is a simple "port" from Py2, then it won't handle
    Python integers either.

    AND

    (b) IF xrange in Py3 is intended to be equivalent to range (which, even in
    Py2, does handle Python integers)

    THEN

    It could be argued that xrange in Py3 needs some attention from the developer(s).

    Stephen Tucker.


    On Thu, Mar 2, 2023 at 6:24 PM Jon Ribbens via Python-list < [email protected]> wrote:

    On 2023-03-02, Stephen Tucker <[email protected]> wrote:
    The range function in Python 2.7 (and yes, I know that it is now superseded), provokes a Memory Error when asked to deiliver a very long list of values.

    I assume that this is because the function produces a list which it then iterates through.

    1. Does the range function in Python 3.x behave the same way?

    No, in Python 3 it is an iterator which produces the next number in the sequence each time.

    2. Is there any equivalent way that behaves more like a for loop (that
    is,
    without producing a list)?

    Yes, 'xrange' in Python 2 behaves like 'range' in Python 3.
    --
    https://mail.python.org/mailman/listinfo/python-list


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Stephen Tucker on Tue Mar 7 17:37:34 2023
    On Tue, 7 Mar 2023 at 16:53, Stephen Tucker <[email protected]> wrote:

    Hi again,

    I tried xrange, but I got an error telling me that my integer was too big
    for a C long.

    Clearly, xrange in Py2 is not capable of dealing with Python (that is, possibly very long) integers.

    That's because Py2 has two different integer types, int and long.

    I am raising this because,

    (a) IF xrange in Py3 is a simple "port" from Py2, then it won't handle
    Python integers either.

    AND

    (b) IF xrange in Py3 is intended to be equivalent to range (which, even in Py2, does handle Python integers)

    THEN

    It could be argued that xrange in Py3 needs some attention from the developer(s).


    Why don't you actually try Python 3 instead of making assumptions
    based on the state of Python from more than a decade ago?

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)