Becoming Unicode Aware
Alex Martelli
aleaxit at yahoo.com
Thu Oct 28 03:36:23 EDT 2004
Michael Foord <fuzzyman at gmail.com> wrote:
...
> def afunction(setoflines, encoding='ascii'):
> for line in setoflines:
> if encoding:
> line = line.decode(encoding)
This snippet as posted is a complicated "no-op but raise an error for
invalidly encoded lines", if it's the whole function.
Assuming the so-called setoflines IS not a set but a list (order
normally matters in such cases), you may rather want:
def afunction(setoflines, encoding='ascii'):
for i, line in enumerate(setoflines):
setoflines[i] = line.decode(encoding)
The removal of the 'if' is just the same advice you were already given;
if you want to be able to explicitly pass encoding='' to AVOID the
decode (the whole purpose of the function), just insert a firs line
if not encoding: return
rather than repeating the test in the loop. But the key change is to
use enumerate to get indices as well as values, and assign into the
indexing in order to update 'setoflines' in-place; assigning to the
local variable 'line' (assuming, again, that you didn't snip your code
w/o a mention of that) is no good.
A good alternative might alternatively be
setoflines[:] = [line.decode(encoding) for line in setoflines]
assuming again that you want the change to happen in-place.
Alex
More information about the Python-list
mailing list