[Python-ideas] Automatic context managers
Bruce Leban
bruce at leapyear.org
Sat Apr 27 00:33:33 CEST 2013
On Fri, Apr 26, 2013 at 1:12 PM, David Mertz <mertz at gnosis.cx> wrote:
> You might get really perverse with stack inspection and whatnot. But I
> was thinking more of just a proxy method that introduced the safety.
>
> class SafeOpen(object):
> def __init__(self, resource):
> self.resource = resource
> def __getattr__(self, name):
> def f(*args, **kws):
> with open(self.resource) as x:
> y = getattr(x, name)(*args, **kws)
> return y
> return f
>
Wouldn't that close the resource before the use function is actually
called? As I read it, it opens, calls getattr(x, 'use'), closes x, then
calls x.use().
--- Bruce
Latest blog post: Alice's Puzzle Page http://www.vroospeak.com
Learn how hackers think: http://j.mp/gruyere-security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130426/c4e8fe83/attachment.html>
More information about the Python-ideas
mailing list