an ugly file-reading pattern

Robin Munn rmunn at pobox.com
Sun Apr 13 01:16:08 EDT 2003


Skip Montanaro <skip at pobox.com> wrote:
> Recent versions of Python (2.2 and later) allow this:
> 
>     file = file("somefile")
>     for line in file:
>         process(line)

Ewww! Don't *do* that! You just shadowed the "file" builtin. Use a
different name for your file object -- I prefer "input_file":

    input_file = file("somefile")
    for line in input_file:
        process(line)

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list