I'm playing with a simple script to escape certain HTML characters, and am encountering a bug which seems to be caused by the order of elements in my list escape_pairs
. I'm not modifying the lists during a loop, so I can't think of any Python/programming principles I'm overlooking here.
escape_pairs = [(">", ">"),("<","<"),('"',"""),("&","&")]
def escape_html(s):
for (i,o) in escape_pairs:
s = s.replace(i,o)
return s
print escape_html(">")
print escape_html("<")
print escape_html('"')
print escape_html("&")
returns
&gt;
&lt;
&quot;
&
However when I switch the order of the elements in my escape_pairs
list to the bug disappears
>>> escape_pairsMod = [("&","&"),("<","<"),('"',"""),(">", ">")]
>
<
"
&