What does "Sparse is better than dense" mean? (Python Zen)

Mark McEahern marklists at mceahern.com
Fri Jul 12 10:59:52 EDT 2002


> Just adding my tuppence worth.  Remember that whitespace can be added
> horizontally too eg.
>
> for line in xreadlines.xreadlines(sys.stdin):
>     if   line[0]  == '#'     : continue
>     elif line[:5] == 'DATE:' : yield ('DATE', scan_date (line))
>     elif line[:5] == 'INTF:' : yield ('INTF', scan_intf (line))
>     elif line[:4] == 'ETH:'  : continue # for now
>     elif line[:4] == '    '  : continue
>     else                     : yield ('DATA', scan_data (line))

For what it's worth, PEP 8 recommends using startswith()/endswith() instead
of slicing for prefix/suffix checking.

Btw, would something like this better express the structure or is it just
obfuscation?

for line in xreadlines.xreadlines(sys.stdin):
  handler = get_handler(line)
  handler.handle(line)

// m

-






More information about the Python-list mailing list