Re: Python 'for' loop is memory inefficient

Discussion in 'Python' started by Emmanuel Surleau, Aug 16, 2009.

  1. > Dr. Phillip M. Feldman wrote:

    [snip]

    > > def is_prime(n):
    > > for j in range(2,n):
    > > if (n % j) == 0: return False
    > > return True
    > >
    > > It seems as though Python is actually expanding range(2,n) into a list of
    > > numbers, even though this is incredibly wasteful of memory. There should
    > > be a looping mechanism that generates the index variable values
    > > incrementally as they are needed.

    >
    > You already have an answer to the range issue, so I will only add that
    > putting a loop inside another loop is a decent way to expand the time
    > taken.
    >
    > I will also observe that if you were to stop programming whatever
    > language you are more familiar with in Python, and start programming
    > Python in Python, you'll have an easier time of it.


    I don't see what's particularly un-Pythonic with this code. Not using xrange()
    is a mistake, certainly, but it remains clear, easily understandable code
    which correctly demonstrates the naive algorithm for detecting whether n is a
    prime. It doesn't call for condescension

    > The Dive Into Python is an excellent start for that.


    I never read it, but I was under the impression that the tutorial on
    python.org was geared toward programmers moving to Python from other
    languages. It was also my impression that Dive Into Python is rather outdated
    and occasionally inaccurate (based on comments on this mailing list).

    Cheers,

    Emm
     
    Emmanuel Surleau, Aug 16, 2009
    #1
    1. Advertising

  2. On Sun, 16 Aug 2009 08:30:54 +0200, Emmanuel Surleau wrote:

    [...]
    >> I will also observe that if you were to stop programming whatever
    >> language you are more familiar with in Python, and start programming
    >> Python in Python, you'll have an easier time of it.

    >
    > I don't see what's particularly un-Pythonic with this code. Not using
    > xrange() is a mistake, certainly, but it remains clear, easily
    > understandable code which correctly demonstrates the naive algorithm for
    > detecting whether n is a prime. It doesn't call for condescension


    It's a particular unfair criticism because the critic (Ethan Furman)
    appears to have made a knee-jerk reaction. The "some language in Python"
    behaviour he's reacting to is the common idiom:

    for i in range(len(seq)):
    do_something_with(seq)


    instead of the "Python in Python" idiom:

    for obj in seq:
    do_something_with(obj)


    That's a reasonable criticism, but *not in the case*. Ethan appears to
    have made the knee-jerk reaction "for i in range() is Bad" without
    stopping to think about what this specific piece of code is actually
    doing.

    (Replace 'obj' with 'j', 'seq' with 'range(2, n)', and
    'do_something_with' with 'if (n % j) == 0: return False', and you have
    the exact same code pattern.)


    --
    Steven
     
    Steven D'Aprano, Aug 16, 2009
    #2
    1. Advertising

  3. > It's a particular unfair criticism because the critic (Ethan Furman)
    > appears to have made a knee-jerk reaction. The "some language in Python"
    > behaviour he's reacting to is the common idiom:
    >
    > for i in range(len(seq)):
    > do_something_with(seq)
    >
    >
    > instead of the "Python in Python" idiom:
    >
    > for obj in seq:
    > do_something_with(obj)
    >
    >
    > That's a reasonable criticism, but *not in the case*. Ethan appears to
    > have made the knee-jerk reaction "for i in range() is Bad" without
    > stopping to think about what this specific piece of code is actually
    > doing.
    >
    > (Replace 'obj' with 'j', 'seq' with 'range(2, n)', and
    > 'do_something_with' with 'if (n % j) == 0: return False', and you have
    > the exact same code pattern.)


    Fair enough. But as far as I know, for i in (x)range(num) is the canonical way
    to iterate over numbers in Python.

    Another case of lack of RTFM* before answering, I suppose.

    Cheers,

    Emm

    *Read The Fine Mail
     
    Emmanuel Surleau, Aug 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. News
    Replies:
    17
    Views:
    453
    Larry Bates
    Apr 14, 2006
  2. Lone Wolf
    Replies:
    5
    Views:
    708
    Dennis Lee Bieber
    Dec 4, 2006
  3. Hendrik van Rooyen

    Re: Python 'for' loop is memory inefficient

    Hendrik van Rooyen, Aug 15, 2009, in forum: Python
    Replies:
    2
    Views:
    262
  4. Steven D'Aprano

    Re: Python 'for' loop is memory inefficient

    Steven D'Aprano, Aug 15, 2009, in forum: Python
    Replies:
    25
    Views:
    1,181
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page