Note that there are some explanatory texts on larger screens.

plurals
  1. POI found myself swinging the list comprehension hammer
    text
    copied!<p>... and every for-loop looked like a list comprehension. </p> <p>Instead of:</p> <pre><code>for stuff in all_stuff: do(stuff) </code></pre> <p>I was doing (not assigning the list to anything):</p> <pre><code>[ do(stuff) for stuff in all_stuff ] </code></pre> <p>This is a common pattern found on <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" rel="nofollow noreferrer">list-comp how-to's</a>. 1) <del>OK, so no big deal right? Wrong.</del> 2) <del>Can't this just be code style? Super wrong.</del></p> <p>1) Yea that was wrong. As NiklasB points out, the of the HowTos is to build up a new list. </p> <p>2) Maybe, but its not obvious and explicit, so better not to use it.</p> <p>I didn't keep in mind that those how-to's were largely command-line based. After my team yelled at me wondering why the hell I was building up massive lists and then letting them go, it occurred to me that I might be introducing a major memory-related bug.</p> <p>So here'er my question/s. 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? When will the garbage collector claim the memory back? After the scope this list is built in is lost?</p> <p>My guess is yes, it will keep consuming my memory. 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 <code>next</code> is called on <code>all_stuff</code>.</p> <p>EDIT. </p> <p>The essence of my question is relayed much cleaner <a href="https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects">in this question</a> (thanks for the link Niklas)</p>
 

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