Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>If I were to do this in a very long running process, where lots of data was being consumed, would this "list" just continue consuming my memory until let go?</p> </blockquote> <p>Absolutely.</p> <blockquote> <p>When will the garbage collector claim the memory back? After the scope this list is built in is lost?</p> </blockquote> <p>CPython uses reference counting, so that is the most likely case. Other implementations work differently, so don't count on it.</p> <p>Thanks to Karl for pointing out that due to the complex memory management mechanisms used by CPython this does <em>not</em> mean that the memory is immediately returned to the OS after that.</p> <blockquote> <p>I don't know how the python garbage collector works, but I would venture to say that this list will exist until after the last next is called on all_stuff.</p> </blockquote> <p>I don't think <em>any</em> garbage collector works like that. Usually they mark-and-sweep, so it could be quite some time before the list is garbage collected.</p> <blockquote> <p>This is a common pattern found on list-comp how-to's.</p> </blockquote> <p>Absolutely not. The point is that you iterate the list with the purpose of doing something with every item (<code>do</code> is called for it's <a href="http://en.wikipedia.org/wiki/Side_effect_(computer_science)" rel="nofollow noreferrer"><em>side-effects</em></a>). In all the examples of the List-comp HOWTO, the list is iterated to <em>build up a new list</em> based on the items of the old one. Let's look at an example:</p> <pre><code># list comp, creates the list [0,1,2,3,4,5,6,7,8,9] [i for i in range(10)] # loop, does nothing for i in range(10): i # meh, just an expression which doesn't have an effect </code></pre> <p>Maybe you'll agree that this loop is utterly senseless, as it doesn't do anything, in contrary to the comprehension, which builds a list. In your example, it's the other way round: The comprehension is completely senseless, because you don't need the list! You can find more information about the issue on a <a href="https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects/5753614#5753614">related question</a></p> <p>By the way, if you really want to write that loop in one line, use a generator consumer like <code>deque.extend</code>. This will be slightly slower than a raw <code>for</code> loop in this simple example, though:</p> <pre><code>&gt;&gt;&gt; from collections import deque &gt;&gt;&gt; consume = deque(maxlen=0).extend &gt;&gt;&gt; consume(do(stuff) for stuff in all_stuff) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload