Too many return-statements = bad style?

Michael Geary Mike at Geary.com
Wed Jul 14 17:23:47 EDT 2004


Mike Hobbs wrote:
> Okay, which of the following examples would you consider to be the best?
>
> 1) (shortest, but f.readline() coded twice)
> f = file(...)
> line = f.readline()
> while line:
>    [ process line from file ]
>    line = f.readline()
> f.close()
>
> [etc.]

I'd vote for this:

f = file(...)
for line in f:
    [ process line from file ]
f.close()

Actually, my favorite is the Ruby version:

File.open(...).each do |line|
    [ process line from file ]
end

-Mike





More information about the Python-list mailing list