[Python-Dev] PEP 343 update (with statement context terminology)

Paul Moore p.f.moore at gmail.com
Mon Apr 24 22:29:41 CEST 2006


On 4/24/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Paul Moore wrote:
> > I've proposed splitting it into
> > two, but that seems not to suit you (you've never responded to it
> > specifically, so I may be misreading your silence here).
>
> Wanting to have two names for the same function tells me there's a problem
> with the terminology, not that we should actually have two names for the same
> function :)

My apologies. On rereading the code, I realised that I'd got muddled
by the decorator implementation. [For anyone reading along, if you
think I'm confused over contexts, watch me try to explain
decorators!!!!] So my explanation was unclear. I'll try again.

In the documentation of contextmanager, consider the examples:

    @contextmanager
    def tag(name):
        ...

    class Tag:
        ...
        @contextmanager
        def __context__(self):
            ...

Now, tag should be a function which returns a context manager (a1
definition - object with a __context__ method) ("with" statement, item
1 of the definition). On the other hand, Tag.__context__ is a context
manager's __context__ method, which according to item 2 of the
definition of the "with" statement, should be a function which returns
a context object (a1 definition - object with __enter__ and __exit__
methods).

*These are two different types of function*.

Just because contextmanager is implemented in such a way that
decorated functions return objects with all 3 methods, doesn't make
the functions conceptually the same.

Does this help you to understand my concern? If not, I give up. (With
one proviso - you insist that objects with __enter__ and __exit__ must
supply __context__. I don't see why that's necessary, but I'll put
that in another post).

Paul.


More information about the Python-Dev mailing list