[Python-ideas] `to_file()` method for strings
Chris Barker
chris.barker at noaa.gov
Mon Mar 28 12:20:18 EDT 2016
On Mon, Mar 28, 2016 at 8:55 AM, Chris Angelico <rosuav at gmail.com> wrote:
> The whole point of the context manager is that it defines the notion
> "go out of scope".
of course it does -- very useful but a lot of extra for a siple read this
file then close it right away, for the casual scripting user :-)
> But if you want the simple syntax, the easiest way
> is to create a helper:
>
> def write_file(filename, contents):
> with open(filename, "w") as f:
> f.write(contents)
>
> write_file('a_file', some_stuff)
>
So I guess this comes around to the OPs point -- a helper like that should
be built in, and easy and obvious to scripting user.
I don't think it should be astring method, but...
Or, if you're writing a quick one-off script, just use the line you
have above, and don't worry about guaranteed closing.
And that comes back around to my original post on this thread -- we should
just tell scripting users to do that :-)
but I think it was Nick's point that Python is used for scripting and
"real" system development, so it's hard to communicate best practices for
both...
given that, I'm kind of liking "write_file"
It solves Nick's mental model issue" " want to write some stuff to a file",
rather than "I want to create a file object, then write stuff to it"
-CHB
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160328/131f63e5/attachment.html>
More information about the Python-ideas
mailing list