Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting
Ben Finney
ben+python at benfinney.id.au
Tue Feb 19 23:42:59 EST 2019
"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:
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
More information about the Python-list
mailing list