[Python-Dev] Proposed resolutions for open PEP 343 issues

Paul Moore p.f.moore at gmail.com
Sun Oct 23 21:15:15 CEST 2005


On 10/23/05, Phillip J. Eby <pje at telecommunity.com> wrote:
> Actually, you've just pointed out a new complication introduced by having
> __context__. The return value of __context__ is supposed to have an
> __enter__ and an __exit__. Is it a type error if it doesn't? How do we
> handle that, exactly?
>> That is, assuming generators don't have enter/exit/context methods, then
> the above code is broken because its __context__ returns an object without
> enter/exit, sort of like an __iter__ that returns something without a 'next()'.

I would have thought that the parallel with __iter__ would be the
right way to go:
>>> class C:
... def __iter__(self):
... return 12
...
>>> c = C()
>>> iter(c)
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
TypeError: __iter__ returned non-iterator of type 'int'
>>>
So, when you try calling __context__ in a with statement (or I guess
in a context() builtin if one were to be added), raise a TypeError if
the resulting object doesn't have __enter__ and __exit__ methods. (Or
maybe just if it has neither - I can't recall if the methods are
optional, but certainly having neither is wrong).
Paul.


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /