[Python-Dev] Merging PEP 310 and PEP 340-redux?
Benji York
benji at zope.com
Thu May 12 06:01:22 CEST 2005
I think this is my first post to python-dev, so a mini-intro: I've
been lurking here for about 5 years, "professional" user a bit longer,
now working at Zope Corp.
Guido van Rossum wrote:
> Going for all-out simplicity, I would like to be able to write these examples:
>
> class locking:
> def __init__(self, lock): self.lock = lock
> def __enter__(self): self.lock.acquire()
> def __exit__(self, *args): self.lock.release()
>
> class opening:
> def __init__(self, filename): self.filename = filename
> def __enter__(self): self.f = open(self.filename); return self.f
> def __exit__(self, *args): self.f.close()
I've read the entire discussion, but may have missed this point, so,
correct me if I'm wrong. Wouldn't these semantics let "normal" objects
be used in a do. For example, if the file object had these methods:
def __enter__(self): return self
def __exit__(self, *args): self.close()
you could write
do file('whatever) as f:
lines = f.readlines()
Or a lock:
def __enter__(self): self.aquire(); return self
def __exit__(self, *args): self.release()
do my_lock:
a()
b()
c()
--
Benji York
Sr. Software Engineer
Zope Corporation
More information about the Python-Dev
mailing list