Too many return-statements = bad style?

gabriele renzi surrender_it at remove.yahoo.it
Thu Jul 15 08:49:43 CEST 2004


il Wed, 14 Jul 2004 16:04:00 -0700, "Michael Geary" <Mike at Geary.com>
ha scritto::

>File.open(...).each do |line|
>    [ process line from file ]
>end
>
>is a closer match to this Python code:
>
>f = file(...)
>try:
>    for line in f:
>        [ process line from file ]
>finally:
>    f.close()
>
>-Mike

it should read:
open(file) do |fd
 ..process fd..
end

note that open() is defined as 'global' in ruby (in Object).

I think the closer match in python should be a method that perfomrs
what you wrote, and accepts a function in input. :
def do_open(name,fun):
  fd=open(name)
  try:
   fun(fd)
  finally:
   fd.close()



More information about the Python-list mailing list