I originally thought Python was a pure pass-by-reference language.
Coming from C/C++ I can't help but think about memory management, and it's hard to put it out of my head. So I'm trying to think of it from a Java perspective and think of everything but primitives as a pass by reference.
Problem: I have a list, containing a bunch of instances of a user defined class.
If I use the for-each syntax, ie.:
for member in my_list:
print(member.str);
Is member
the equivalent of an actual reference to the object?
Is it the equivalent of doing:
i = 0
while i < len(my_list):
print(my_list[i])
i += 1
I think it's NOT, because when I'm looking to do a replace, it doesn't work, that is, this doesn't work:
for member in my_list:
if member == some_other_obj:
member = some_other_obj
A simple find and replace in a list. Can that be done in a for-each loop, if so, how? Else, do I simply have to use the random access syntax (square brackets), or will NEITHER work and I need to remove the entry, and insert a new one? I.e.:
i = 0
for member in my_list:
if member == some_other_obj:
my_list.remove(i)
my_list.insert(i, member)
i += 1