Re: Good Python style?



On Thu, 31 May 2007 09:59:07 +0200, Andreas Beyer wrote:

Hi,

I found the following quite cryptic code, which basically reads the
first column of some_file into a set.
In Python I am used to seeing much more verbose/explicit code. However,
the example below _may_ actually be faster than the usual "for line in ..."
Do you consider this code good Python style? Or would you recommend to
refrain from such complex single-line code??

Thanks!
Andreas

inp = resource(some_file)
# read first entries of all non-empty lines into a set
some_set = frozenset([line.split()[0] for line in \
filter(None, [ln.strip() for ln in inp])])

It's a little complex, but not excessively so. Any more, and it would
probably be too complex.

It would probably be easier to read with more readable names and a few
comments:

some_set = frozenset(
# ... from a list comp of the first word in each line
[line.split()[0] for line in
# ... each line has leading and trailing white space
# filtered out, and blanks are skipped
filter(None, # strip whitespace and filter out blank lines
[line.strip() for line in input_lines]
)])

Splitting it into multiple lines is self-documenting:

blankless_lines = filter(None, [line.strip() for line in input_lines])
first_words = [line.split()[0] for line in blankless_words]
some_set = frozenset(first_words)

As for which is faster, I doubt that there will be much difference, but I
expect the first version will be a smidgen fastest. However, I'm too lazy
to time it myself, so I'll just point you at the timeit module.



--
Steven.

.



Relevant Pages

  • Re: Good Python style?
    ... I found the following quite cryptic code, ... first column of some_file into a set. ... In Python I am used to seeing much more verbose/explicit code. ... for line in inp ...
    (comp.lang.python)
  • Good Python style?
    ... I found the following quite cryptic code, ... first column of some_file into a set. ... In Python I am used to seeing much more verbose/explicit code. ... # read first entries of all non-empty lines into a set ...
    (comp.lang.python)
  • Re: Why python doesnt use syntax like function(,,x) for default parameters?
    ... def ChooseItems(StartDate, EndDate, Filter): ... ChooseItems(, '01.01.2000') #get everything before a date ... Now look at your example rewritten with standard Python keyword syntax. ...
    (comp.lang.python)
  • Re: Python becoming less Lisp-like
    ... > Basically, it says that it will get rid of the explicit map, filter ... > It will also get rid of lambda, and it's not a great loss, since ... > The real problem with Python is that it has been very successful as a ...
    (comp.lang.python)
  • Re: Python becoming less Lisp-like
    ... > Basically, it says that it will get rid of the explicit map, filter ... > It will also get rid of lambda, and it's not a great loss, since ... > The real problem with Python is that it has been very successful as a ...
    (comp.lang.lisp)