[Python-ideas] Adding __getter__ to compliment __iter__.
Ron Adam
ron3200 at gmail.com
Sat Jul 20 23:17:26 CEST 2013
On 07/19/2013 12:32 PM, Steven D'Aprano wrote:
> On 20/07/13 01:58, Ron Adam wrote:
>
>> Could we have syntax for generators to bypass the method calls?
>>
>> x = gen[] # next
>
> Please no.
>
> What's so special about generators that they should have magic syntax for
> bypassing methods?
Generators are quite special. Because they suspend and resume, and the
values that are passed on each yield are limited to a single object in
followed by a single object out.
> Good uses for syntax include doing things that you can't
> otherwise do, not as a mere alias for a method.
This is what I was trying to convey.
Inside a generator, a yield expression is the following byte code.
"x = yield y"
0 LOAD_FAST 0 (y)
3 YIELD_VALUE
4 STORE_FAST 1 (x)
And it's external counter point is...
"y = g.send(x)"
0 LOAD_GLOBAL 0 (g)
3 LOAD_ATTR 1 (send)
6 LOAD_FAST 0 (x)
9 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
12 STORE_FAST 1 (y)
My question is can we make the above to be this?
0 LOAD_GLOBAL 0 (g)
6 LOAD_FAST 0 (x)
9 RESUME_YIELD
12 STORE_FAST 1 (y)
It avoids the attribute lookup, and passing the values through the function
call and signature parsing code in ceval.c.
Cheers,
Ron
More information about the Python-ideas
mailing list