Confused with csv.reader copies

Robert Dailey rcdailey at gmail.com
Mon Jul 23 14:45:42 EDT 2007


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.




More information about the Python-list mailing list