[Python-Dev] PEP 343 - Abstract Block Redux

Greg Ewing greg.ewing at canterbury.ac.nz
Mon May 16 08:11:50 CEST 2005


Nick Coghlan wrote:

> The naming convention for 'do' is shown in the current PEP 343. The issue I've 
> noticed with it is that *functions* read well, but methods don't because things 
> get out of sequence. That is, "do locking(the_lock)" reads well, but "do 
> the_lock.locking()" does not.
> 
> Whereas, using 'with', it can be written either way, and still read reasonably 
> well ("with locked(the_lock)", "with the_lock.locked()").
> 
> The 'with' keyword also reads better if objects natively support use in 'with' 
> blocks ("with the_lock", "with the_file").
> 
> Guido's concern regarding file objects being reused inappropriately can be dealt 
> with in the file __enter__ method:
> 
>    def __enter__(self):
>        if self.closed:
>            raise RuntimeError, "Cannot reopen closed file handle"
> 
> For files, it may then become the common practice to keep pathnames around, 
> rather than open file handles. When you actually needed access to the file, the 
> existing "open" builtin would suffice:
> 
>    with open(filename, "rb") as f:
>        for line in f:
>            print line

I think I'm starting to agree. Currently about +0.6 in
favour of 'with' now, especially if this is to be
almost exclusively a resource-acquisition statement,
as all our use cases seem to be.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+


More information about the Python-Dev mailing list