Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6085913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:41:34+00:00 2026-05-23T11:41:34+00:00

We know the following code is loading the data line-by-line only rather than loading

  • 0

We know the following code is loading the data line-by-line only rather than loading them all in memory. i.e. the line alread read will be somehow marked ‘deletable’ for the OS

def fileGen( file ):
    for line in file:
        yield line

with open("somefile") as file:
    for line in fileGen( file ):
        print line

but is there anyway we could verify if this is still true if we modify the definition of fileGen to following?

def fileGen( file ):
    for line in csv.Reader( file ):
        yield line

How we could know if csv.Reader will cache the data it loaded? thanks

regards,
John

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:41:34+00:00Added an answer on May 23, 2026 at 11:41 am

    The most reliable way to find out what csv.reader is doing is to read the source. See _csv.c, lines 773 onwards. You’ll see that the reader object has a pointer to the underlying iterator (typically a file iterator), and it calls PyIter_Next each time it needs another line. So it does not read ahead or otherwise cache the data it loads.

    Another way to find out what csv.reader is doing is to make a mock file object that can report when it is being queried. For example:

    class MockFile:
        def __init__(self): self.line = 0
        def __iter__(self): return self
        def next(self):
            self.line += 1
            print "MockFile line", self.line
            return "line,{0}".format(self.line)
    
    >>> r = csv.reader(MockFile())
    >>> next(r)
    MockFile line 1
    ['line', '1']
    >>> next(r)
    MockFile line 2
    ['line', '2']
    

    This confirms what we learned from reading the csv source code: it only requests the next line from the underlying iterator when its own next method is called.


    John made it clear (see comments) that his concern is whether csv.reader keeps the lines alive, preventing them from being collected by Python’s memory manager.

    Again, you can either read the code (most reliable) or try an experiment. If you look at the implementation of Reader_iternext in _csv.c, you’ll see that lineobj is the name given to the object returned by the underlying iterator, and there’s a call to Py_DECREF(lineobj) on every path through the code. So csv.reader does not keep lineobj alive.

    Here’s an experiment to confirm that.

    class FinalizableString(string):
        """A string that reports its deletion."""
        def __init__(self, s): self.s = s
        def __str__(self): return self.s
        def __del__(self): print "*** Deleting", self.s
    
    class MockFile:
        def __init__(self): self.line = 0
        def __iter__(self): return self
        def next(self):
            self.line += 1
            return FinalizableString("line,{0}".format(self.line))
    
    >>> r = csv.reader(MockFile())
    >>> next(r)
    *** Deleting line,1
    ['line', '1']
    >>> next(r)
    *** Deleting line,2
    ['line', '2']
    

    So you can see that csv.reader does not hang on to the objects it gets from its iterator, and if nothing else is keeping them alive, then they get garbage-collected in a timely fashion.


    I have a feeling that there’s something more to this question that you’re not telling us. Can you explain why you are worried about this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know why the following code only has effect in FF? $(document).ready(function() {
I know the following code could extract whole texts of the docx document, however,
I know that the following code should show and hide a tiny circular progress
I know why the following code doesn't compile: public class Main { public static
I'm curious to know why the following code behaves differently? The following does not
I wanted to know why the following code crashes. int main( ) { int
I know how to use INDEX as in the following code. And I know
Does anyone know how I can get rid of the following assembler warning? Code
I have following code on my site: backgroundImages[bg_img_path_b]=new Image(); backgroundImages[bg_img_path_b].src = bg_img_path_b; backgroundImages[bg_img_path_b].loaded=loading; //jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
I'm using subgrid for jqgrid. It's 1 level. Following is my code.I don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.