Confused with csv.reader copies

Larry Bates larry.bates at websafe.com
Mon Jul 23 15:28:04 EDT 2007


Robert Dailey wrote:
> First, take a look at my example code:
> -----------------------------------------------------
> import csv
> 
> def pass1( reader ):
> 	print reader.next()
> 	print reader.next()
> 
> def pass2( reader ):
> 	print reader.next()
> 	print reader.next()
> 
> reader = csv.reader( open( "C:/IT/Method/SpaceImpact/code/tools/
> ProfileViewer/performance_profile.csv", "rb" ) )
> 
> pass1( reader )
> pass2( reader )
> -----------------------------------------------------
> 
> The output is as follows:
> -----------------------------------------------------
> ['0', 'root', '00:01:32.793591', '1']
> ['1', 'Engine::Tick', '00:00:25.886411', '1851']
> ['2', 'Sprite::Tick', '00:00:00.001495', '385']
> ['2', 'Entity::Tick', '00:00:00.001485', '45']
> -----------------------------------------------------
> 
> I expected the output to be this:
> -----------------------------------------------------
> ['0', 'root', '00:01:32.793591', '1']
> ['1', 'Engine::Tick', '00:00:25.886411', '1851']
> ['0', 'root', '00:01:32.793591', '1']
> ['1', 'Engine::Tick', '00:00:25.886411', '1851']
> -----------------------------------------------------
> 
> My understanding is that objects are passed by reference, meaning
> there is no hard copy of the data, however the copies passed to
> functions do not affect the version passed in. In other words, when I
> call "next" on the reference passed into each function, it should not
> affect the variable that was originally passed in.
> 
> I'm attempting to use recursion to build a TreeCtrl in wxPython using
> this data, and I can't get it to work properly if the variable outside
> of the function call ends up having its state (e.g., its "next"
> pointer) modified by passing it into other functions.
> 
> Any tips on getting this to work? I'm a native C++ programmer still
> learning Python, so I apologize for any confusion. Thanks.
> 

By reference means that mutable objects can be modified (in place) by the
functions that they are passed into.  This is a common mistake people make when
they first get started.  Normally lists seem to throw people first.
I don't know how long the file is, but you probably should just read the entire
file into memory and process it from there.

-Larry



More information about the Python-list mailing list