Paolino wrote: > Steven D'Aprano wrote: > > > def translate(text): > > import string > > all=string.maketrans('','') > > badcars=all.translate(all,string.letters+string.digits) > > table=string.maketrans(badcars,'_'*len(badcars)) > > return text.translate(table) > > > > No pollution. > > And no efficience.Recalculating all,badcars and table was not an > acceptable solution ,sorry if I didn't state this point :(
Then write a closure. You get both encapsulation and efficience, and as a bonus, customization of the translating function: import string def translateFactory(validChars=string.letters+string.digits, replaceChar='_'): all=string.maketrans('','') badcars=all.translate(all,validChars) table=string.maketrans(badcars, replaceChar*len(badcars)) def translate(text): return text.translate(table) # bind any attributes you want to be accessible # translate.badcars = badcars # ... return translate tr = translateFactory() tr("Hel\xfflo") George -- http://mail.python.org/mailman/listinfo/python-list