[Python-ideas] `to_file()` method for strings

Nick Eubank nickeubank at gmail.com
Mon Mar 28 14:16:31 EDT 2016


Just to make sure I follow: Chris, you're proposing a modification to
garbage collection to clean up `open()` if `open()` is used by not closed
and not assigned to anything so that `open(file).write(stuff)` is safe?

That sounds brilliant to me, and totally meets my goal of having a
"one-step" method for writing to disk that doesn't require users to develop
a two-step mental model. If it's feasible (I have nothing but the most
basic understanding of how the garbage collector works), that would totally
obviate my desire to have a `to_file()` method on strings.

On Mon, Mar 28, 2016 at 8:46 AM Chris Barker <chris.barker at noaa.gov> wrote:

> On Thu, Mar 24, 2016 at 7:22 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
>> One of the few downsides of Python's popularity as both a scripting
>> language and an app development language is that a lot of tutorials
>> are written for the latter, and in app development, relying on the GC
>> for external resource cleanup isn't a great habit to get into.
>
>
> no, it's not. though now that think about it, while I understand that
> context managers are great as a universal and flexible way to ,well, mange
> context, for file objects themselves:
>
> open('a_file', 'w').write(some_stuff)
>
> Is really nice -- while we don't want to make reference-counting garbage
> collection a required part of the language in general, I wonder if it would
> be practical to make it part of the spec for SOME objects -- i.e kind of
> like you can define a given object to be a context manager, you could
> define certain objects to clean up after themselves when they go out of
> scope. It would sure be nice is this example ;; it would buy us that simple
> syntax, the atomic operation, etc...
>
> I have not thought this through AT ALL, but I wonder if one could avoid
> requiring a reference counting system by catching at the parsing stage that
> that the object created is never assigned to anything -- and this you know
> it's going to go out of scope as soon as that line is finished processing.
>
> In fact, if you have a non-reference counting garbage collection system,
> it might be helpful to special case all objects with very short lives, so
> you don't pile up a bunch of temporaries, etc that have to be collected
> later....
>
> -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/efe65ee8/attachment-0001.html>


More information about the Python-ideas mailing list