[Python-ideas] Adding __getter__ to compliment __iter__.

Steven D'Aprano steve at pearwood.info
Sun Jul 21 02:47:51 CEST 2013


On 21/07/13 07:17, Ron Adam wrote:
>
>
> 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.

I know what generators do. I asked, what is so special that they need *syntax for bypassing methods*. That's the part that you didn't answer. Your syntax suggestion doesn't change either the fact that they suspend and resume, or that the values passed are limited to a single object. We already have an idiom for passing multiple objects at a time: the tuple.


>> 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.

But your proposal is exactly that, a mere alias. It doesn't add any new functionality. It doesn't let you do anything that can't already be done. That's my point. Instead of things that read like Python code and that you can easily look up in the docs:

next(gen)
gen.send(x)

you have these mysterious syntax, one of which looks like a key/item lookup missing an argument, and one of which just looks like a key/item lookup:

gen[]
gen[x]

I haven't even mentioned that this proposal can't fly because the Python compiler cannot tell ahead of time which code is intended. You could get around that by changing the syntax:

gen!!
gen!x!

I'm questioning the need for this to be syntax in the first place.



-- 
Steven


More information about the Python-ideas mailing list