[Python-ideas] Adding __getter__ to compliment __iter__.

Ron Adam ron3200 at gmail.com
Thu Jul 18 15:59:45 CEST 2013


On 07/18/2013 03:56 AM, Joshua Landau wrote:
> On 18 July 2013 07:12, Ron Adam <ron3200 at gmail.com> wrote:
>>
>>    def extend_items(A, B):
>>       """ Add the content of the sub_items in B to A. """
>>       # A must be mutable for this function to work.
>>       g = A.getter()    # g is an input interface to "A"
>>       for sub_list in B:
>>           g.isend(sub_list)   # inserts the contents of sub_list into A
>
> 1) Say that A is a tuple -- how do you get the "aggregate" tuple out
> from this at the end?

You can't in this example.  Which is why the comment is there.  This 
function isn't meant to be in the proposal,  the proposal allow you to 
write functions like this easier.

A mutable version of this.. that also would work with immutables would be...

     def extend_items(A, B):
         """ Add the content of the sub_items in B to A. """
         for sub_list in B:
             g = A.getter()          # A Obj changes on each loop.
             A = g.isend(sub_list)   # new A copy here.
         return A


> 2) Personally, I just don't see enough of a use-case to want this. Our
> standard "universal duck-types" includes "+" which does the same thing
> for most containers.

And we have slicing too.  It's a suggestion, and there may not be enough in 
it to justify it.  But they just don't work on as many things as iter() 
does for getting things out of containers.  So I think something like this 
is a nice addition.


> 3) It needs better names.

Yes, getter is temporary, I'm not worried about the name.


> 4) Passing in a dictionary is odd, because iter(dictionary) iterates
> over keys; If you're passing in an iterable (hence "i"send) that
> wouldn't work great. It just seems inconsistent in a way.

Yes, that's unfortunate.  You need to call the dictionaries .items() method 
in this case.    g = getter(dict.items())  It is a view, so it would still 
work.


> 5) Chain works fine most of the time I could imagine wanting this.

Chain is a the other direction.  It gets stuff out of a list of things by 
using iter().  A getter()'s would create that same flexibility for putting 
things into containers.

Cheers,
    Ron









More information about the Python-ideas mailing list