[Python-Dev] PEP 340 -- concept clarification

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed May 4 04:30:55 CEST 2005


Delaney, Timothy C (Timothy) wrote:

> Guido van Rossum wrote:
> 
>> I'd like the block statement to be defined exclusively in terms of
>> __exit__() though.
> 
> 1. If an iterator declares __exit__, it cannot be used in a for-loop.
>    For-loops do not guarantee resource cleanup.
> 
> 2. If an iterator does not declare __exit__, it cannot be used in a
> block-statement.
>    Block-statements guarantee resource cleanup.

Now some thoughts have solidified in my mind ... I'd like to define some
terminology that may be useful.

resource protocol:
    __next__
    __exit__

    Note: __iter__ is explicitly *not* required.

resource:
    An object that conforms to the resource protocol.

resource generator:
    A generator function that produces a resource.

resource usage statement/suite:
    A suite that uses a resource.

With this conceptual framework, I think the following makes sense:

- Keyword 'resource' for defining a resource generator.
- Keyword 'use' for using a resource.

e.g.

::

    resource locker (lock):
        lock.acquire()
        try:
            yield
        finally:
            lock.release()

    use locker(lock):
        # do stuff

Tim Delaney


More information about the Python-Dev mailing list