例如,假设我想要一个函数来转义字符串以在HTML中使用(如在Django的escapefilter中):defescape(string):"""Returnsthegivenstringwithampersands,quotesandanglebracketsencoded."""returnstring.replace('&','&').replace('','>').replace("'",''').replace('"','"')这可行,但它很快变得丑陋并且算法性能似乎很差(在这个例子中,字符串被重复遍历了5次)。最好是这样的:defescape