Ok…that makes sense as far as what [:] does and why it might be needed.
However it’s still not clear to me why it might be needed in this exercise. Here sentence is something that changes with every iteration of the for loop and it changes after result was appended to results so there appears to be no opportunity in the code for cross contamination of sentence if something changes in result. Consider this modification where I removed [:] (and also added more print statements to track the execution)…
for sentence in snippet, phrase:
print("sentence (before result defined): ", sentence)
# this is how you duplicate a list or string
result = sentence
print("result: ", result)
print("sentence (after result defined): ", sentence)
# replaces any instance of %%% in the snippet and corresponding
# phrase with a class_name if class_names has anything in its list
for word in class_names:
result = result.replace("%%%", word, 1)
print("class_names result: ", result)
print("class_names sentence: ", sentence)
# replaces any instance of *** in the snippet and corresponding
# phrase with an other_name if other_names has anything in its list
for word in other_names:
result = result.replace("***", word, 1)
print("other_names result: ", result)
print("other_names sentence: ", sentence)
# replaces any instance of @@@ in the snippet and corresponding
# phrase with a param_name if param_names has anything in its list
for word in param_names:
result = result.replace("@@@", word, 1)
print("param_names result: ", result)
print("param_names sentence: ", sentence)
results.append(result)
print("results: ", results)
print("result (post results.append(result)): ", result)
print("sentence (post results.append(result)): ", sentence)
results in this in Powershell:
PS C:\Users\dswal\AppData\Local\Programs\Python\Python36\Doug> python oop_test.py english
sentence (before result defined): ***.***(@@@)
result: ***.***(@@@)
sentence after result defined: ***.***(@@@)
other_names result: boot.***(@@@)
other_names sentence: ***.***(@@@)
other_names result: boot.believe(@@@)
other_names sentence: ***.***(@@@)
param_names result: boot.believe(beam, duck)
param_names sentence: ***.***(@@@)
results: ['boot.believe(beam, duck)']
result (post results.append(result)): boot.believe(beam, duck)
sentence (post results.append(result)): ***.***(@@@)
sentence (before result defined): From *** get the *** function, call it with params self, @@@.
result: From *** get the *** function, call it with params self, @@@.
sentence after result defined: From *** get the *** function, call it with params self, @@@.
other_names result: From boot get the *** function, call it with params self, @@@.
other_names sentence: From *** get the *** function, call it with params self, @@@.
other_names result: From boot get the believe function, call it with params self, @@@.
other_names sentence: From *** get the *** function, call it with params self, @@@.
param_names result: From boot get the believe function, call it with params self, beam, duck.
param_names sentence: From *** get the *** function, call it with params self, @@@.
results: ['boot.believe(beam, duck)', 'From boot get the believe function, call it with params self, beam, duck.']
result (post results.append(result)): From boot get the believe function, call it with params self, beam, duck.
sentence (post results.append(result)): From *** get the *** function, call it with params self, @@@.
From boot get the believe function, call it with params self, beam, duck.
As you can see sentence never changes its value even though result does. And that’s with [:] removed from ressult = sentence[:].
So with [:] included or not, the results don’t seem to change. So why use it here?
Also consider this modification where I removed result from the code and just used sentence in its place:
for sentence in snippet, phrase:
# replaces any instance of %%% in the snippet and corresponding
# phrase with a class_name if class_names has anything in its list
for word in class_names:
sentence = sentence.replace("%%%", word, 1)
# replaces any instance of *** in the snippet and corresponding
# phrase with an other_name if other_names has anything in its list
for word in other_names:
sentence = sentence.replace("***", word, 1)
# replaces any instance of @@@ in the snippet and corresponding
# phrase with a param_name if param_names has anything in its list
for word in param_names:
sentence = sentence.replace("@@@", word, 1)
results.append(sentence)
This change also appears to work just fine with the code. So I’m back where I started at the beginning of this thread wondering why result = sentence[:] is even in the code because it seems to be superfluous to the code functioning correctly and we could use the code with sentence as the only variable needed. What am I missing here?