"mnl.p...@gmail.com" <mnl.p...@gmail.com> writes:

> with xxxxxx.open() as logfile:
>      do this
>      do that
>      logfile.write()
>      do this
>      do that
>      logfile.write()

That's a good sign you have identified a discrete collection of
statements that should be in a function. The function accepts
‘logfile’ as a (probably named) parameter, so that it is explit the
opening and closing of that file is someone else's responsibility This,
as you say, simplifies the code in those statements.

Bonus: you can name the function to summarise its entire purpose::

    with foo.open() as logfile:
        frobnicate_the_whelk(logfile=logfile)

-- 
 \      “I don't want to live peacefully with difficult realities, and |
  `\     I see no virtue in savoring excuses for avoiding a search for |
_o__)                        real answers.” —Paul Z. Myers, 2009-09-12 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to