basic question: target assignment in for loop

Kawaldeep Grewal grewal at uclink.berzerkeley.edu
Mon Feb 24 02:11:02 EST 2003


hello,

this may be a faq, and if it is, I would appreciate a pointer in the 
right direction.

I'm using python to edit some text/html with the re module. I want to do 
this:

html = htmlFile.readlines()
for line in html:
       line = re.sub("regexString", functionReturningString, line),

but python assigns the target by value and not by reference, which (in 
my mind) breaks the abstraction. So, I have to resort to this:

html = htmlFile.readlines()
i = 0
while i < len(html):
        html[i] = re.sub("regexString", functionReturningString, html[i])
        i = i + 1

this code is decidedly not elegant, and looks very C-ish. As I'm new to 
python, can anyone tell me whether I'm just confused or that this is the 
way to do things?


thanks,

Kawaldeep

-- 
if you desire to correspond via email, please replace berzerkeley with berkeley




More information about the Python-list mailing list