[Tutor] A Python idiom that I don't get
w chun
wescpy at gmail.com
Wed Apr 26 05:16:36 CEST 2006
> > prefix = os.path.commonprefix(filter( bool, lines ))
that is an interesting and yes, not very optimal way of saving the set
of non-blank lines. the commonprefix() function takes a list of
pathnames and returns the longest prefix that all strings have in
common, presumably for disk filenames, although this sounds like just
a string processing function that would work with non-pathnames too.
> > and I don't understand what that 'bool' is doing. Or rather, I think
> > that I see what it is doing, but I am not sure - and I don't much like it.
> :
> > I tried replacing 'bool'...
it sounds like you did not develop the original code. it seems to
work... why are you trying to replace it? are you refactoring?
> This works but it isn't doing what you think it is.
> lambda True: True
> is the same as
> lambda x: x
kent is right. True is not a keyword, so you are just using "True" as
a variable name. scarily, one can do stuff like this:
>>> True, False = False, True
>>> True
False
>>> False
True
> Try filter(None, lines) or use a list comprehension:
> [ line for line in lines if line ]
i like both of these better... they will perform better than with
bool(). of the two, it would be interesting to study which one's
faster... and why.
-wesley
More information about the Tutor
mailing list