[Python-ideas] Revised**5 PEP on yield-from
jh at improva.dk
Sat Feb 21 10:06:43 CET 2009
Greg Ewing wrote:
> I'm no longer sure that the implementation I'm working
> on can be exactly described in terms of any Python
> expansion, anyway.
> One problem is that if the generator gets None from
> a yield, it has no way of knowing whether it came from
> a next() or a send(None),
This is only a problem with the recursive implementation though, isn't it?
> so it doesn't know which
> method to call on the subiterator. The best that it
> could do is
> if _v is None:
> _u = _i.next()
> _u = _i.send(_v)
> but I would have to modify my implementation as it
> currently stands to make it work like that.
> This may be the right thing to do, as it would bring
> things into line with the advertised equivalence of
> next() and send(None), and you would still get an
> exception if you tried to send something non-None to
> an object without a send() method.
I still (see example in another thread) think that a missing 'send'
treated as a 'next'. To me, the "communicating directly with the caller" bit
is less important than the argument that the caller is still talking to a
generator that *has* a send but may ignore the values sent.
>> I strongly suspect that the lower bound for this problem is
>> per operation, and I can see a number of ways to do it in O(logn) time.
> Before you bust too many neurons on O() calculations,
> be aware that the constant factors are important here.
> I'm relying on traversing a delegation chain using
> C calls taking negligible time compared to doing it
> in Python, so that it can be treated as a constant-time
> operation for all practical purposes regardless of the
> length of the chain. Testing will reveal whether this
> turns out to be true in real life...
Too late, but don't worry. I'm doing this for fun :)
I think I can actually see a way to do it that should be fast enough,
but I'd like to
work out the details first. If it works it will be O(1) with low
constants as long as
you don't build trees, and similar to traversing a delegation chain in
the worst case.
All this depends on getting it working using delegation chains first
though, as most of
the StopIteration and Exception handling would be the same.
More information about the Python-ideas