[Python-ideas] yield from __setitem__ and __setattr__

Martin Teichmann lkb.teichmann at gmail.com
Tue Sep 9 19:36:35 CEST 2014


Hi list,

I am a happy user of yield from, especially while also using asyncio.
A great feature is that one can seamlessly convert synchronous code
into asynchronous code by adding the appropriate yield froms and
coroutine statements at the proper place, including special methods
like __getitem__, as in "yield from something[7]".

But we cannot yield from the special methods __setattr__ and
__setitem__. That is unfortunate, as I am programming a proxy object
for something remote over the network, and also __setitem__ might
take some time. So I got the idea that yield from might also be used
on the left side of an assigment, as in

  yield from o[3], yield from o.something = "a", "b"

which should be equivalent to

yield from o.__setitem__(3, "a")
yield from o.__setattr__("something", "b")

I know that with this proposal I am probably far up on the list of most
ugly syntax proposed ever, so maybe someone has a better idea.


As a side note: with asyncio I am using yield from a lot, and I think it
is a pity that it is so long an clumsy a keyword-couple. Couln't it be
shortened? I'm thinking about just using "from", especially in an asyncio
context, "from sleep(3)" sounds bad to me, but still better than "yield
from sleep(3)".
Or maybe we could have an auto-yielder, by just declaring that every
expression that evaluates to a coroutine is automatically yielded from,
as long as the calling function is a coroutine itself.

Greetings

Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140909/d90d2066/attachment.html>


More information about the Python-ideas mailing list