I am trying to get some existing CPython 2.4 code working under Jython (2.1) and I am puzzled by a line in the following function. It seems to be a Python 2.4 idiom that is opaque to me.
The line is: prefix = os.path.commonprefix(filter( bool, lines )) 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. filter is the built-in filter and it requires a callable returning a bool as the first argument. It seems that 'bool' without arguments is a callable that always evaluates to True (or at least non-zero) so this 'bool' always returns True. Is this really true (sic) by intention or is it just an implemenation artifact? I tried replacing 'bool' with 'True' but that won't work because True is not callable. I replaced 'bool' with 'lambda True: True' as in: prefix = os.path.commonprefix(filter( lambda True: True, lines )) and that does seem to work - and pass its unit tests. Have I got this right and can I replace 'bool' with the lambda expression? Or is there a clearer way to do this? Thanks, Don. The full function is: def find_common( lines ): """find and return a common prefix to all the passed lines. Should not include trailing spaces """ if not lines: return "" # get the common prefix of the non-blank lines. This isn't the most # efficient way of doing this by _far_, but it _is_ the shortest prefix = os.path.commonprefix(filter( bool, lines )) if not prefix: return "" # this regular expression will match an 'interesting' line prefix matched = re.match("(\s*\w{1,4}>|\W+)", prefix) if not matched: return "" _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor