[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting

"mnl.post at gmail.com" <mnl.post at 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:

 \      ?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