object.enable() anti-pattern

Michael Speer knomenet at gmail.com
Thu May 9 20:18:39 EDT 2013


By his reasoning it simply shouldn't exist. Instead you would access the
information only like this:

with open("myfile.dat") as f:
  data = f.read()

Which is my preferred way to work with resources requiring cleanup in
python anyways, as it ensures I have the least chance of messing things up,
and that all of my resources are disposed of properly during the unwind.
It's a hell of a lot cleaner than remembering to call the appropriate
cleanup functions at every callsite, and the resultant values can never be
in a bad state ( at least due to programmer function-ordering fault,
underlying file i/o errors and things can still cause errors, but not due
to API mis-usage ).

Python 3's multiple-with-statement-target syntax was backported to 2.7 as
well, flattening the deep nests of with statements otherwise needed to
implement this pattern
    http://docs.python.org/dev/whatsnew/2.7.html#other-language-changes

On Thu, May 9, 2013 at 7:43 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz>wrote:

> Wayne Werner wrote:
>
>> You don't ever want a class that has functions that need to be called in
>> a certain order to *not* crash.
>>
>
> That seems like an overly broad statement. What
> do you think the following should do?
>
>    f = open("myfile.dat")
>    f.close()
>    data = f.read()
>
>
> --
> Greg
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130509/bf575f48/attachment.html>


More information about the Python-list mailing list