[Python-Dev] Use for enumerate (original) (raw)

Ben Wolfson wolfson@midway.uchicago.edu
Fri, 26 Apr 2002 22:21:55 -0500 (CDT)


(I'm not subscribed so this won't thread right, but oh well)

This doesn't preserve the same semantics as the original, in that it always reads the entire file, but it is awfully short:

def getline(filename, lineno): return (lineno < 1 and ('',) or ([ln for (i,ln) in enumerate(open(filename)) if i==lineno-1] or ('',)))[0]

-- BTR
YOU HEAR ME, BEN WOLFSON?!? YOUR SUPERCILIOUS CONTEMPT POWERS CANNOT WITHSTAND THE POWER OF MY BOORISH DIMWIT RAY!!!! -- revjack