"sins" (aka, acknowledged language problems)

Fredrik Lundh fredrik at pythonware.com
Fri Dec 17 05:28:16 EST 1999


Alex Martelli <Alex.Martelli at think3.com> wrote:
> I cannot help but keep staring at the result:
> 
> import sys
> 
> def copy(regex, dict, inp=sys.stdin, oup=sys.stdout):
>     def repl(match,dict=dict):
>         return eval(match.group(1),dict)
>     while 1:
>         line = inp.readline()
>         if not line:
>             break
>         oup.write(regex.sub(repl,line))
> 
> and wonder that *FOUR*-count-em lines of
> this function, fully half of it, are devoted to
> the trivial, frequent, repetitive task of looping
> over input lines, reading each one, bailing
> out when done.

hmm.  by some reason, my brain treats that
while/if/break stuff as one large "glyph".  am
I the only one here who has a brain equipped
with a 2D pattern matcher? ;-)

> But why can't I change those 4 lines to, say:
>     while line := inp.readline():

how about:

import fileinput
for line in fileinput.input():
    ...

</F>





More information about the Python-list mailing list