I have strings stored in various objects, for example a, b, c[2:7] (ie 5 different strings c[2],…c[6]), d.s, e.s[:] (ie all strings stored in the e.s list). I need to loop over all these strings on various places in the code and modify these variables in the loops. (Assigning the modified strings to new variables would not work as there are other functions expecting the modified strings in the original variables.) How can I do that efficiently in Python?
If I could create a list of references to the strings then I could just always iterate over it:
str_ref_lst=[reference(a), reference(b), reference(d.s)]
str_ref_lst.extend[reference(c[2:7])]
str_ref_lst.extend[reference(e.s[:])]
...
for ref_strng in str_ref_lst:
do_string_modifications1(dereference(ref_strng))
...
function_using_the_modified_variables1()
...
for ref_strng in str_ref_lst:
do_string_modifications2(dereference(ref_strng))
...
function_using_the_modified_variables2()
etc
However, I am not aware of reference()/dereference() analogues in Python (and I guess it would not be the right way in Python anyway)
The recommended answer should be “use a dictionary”, but if you have to do it that way:
IIRC this way doesn’t work in Python 3.0 because of internal optimizations
With slices, it should be somewhat like