[Python-ideas] Adding __getter__ to compliment __iter__.

Clay Sweetser clay.sweetser at gmail.com
Thu Jul 18 08:51:08 CEST 2013


What is the difference between this "getter protocol" and using a
generator's send method?
On Jul 18, 2013 2:47 AM, "Ron Adam" <ron3200 at gmail.com> wrote:

>
>
> These methods would be called by a getter function which starts it by
> calling next on it before returning it.
>
>
> def getter(container):
>     """ Get a getter from a container object. """
>     g = container.__getter__()
>     next(g)
>     return g
>
>
> On 07/18/2013 01:12 AM, Ron Adam wrote:
>
>>
>>
>> A __getter__ method on a list object might be..
>>
>>     def __getter__(self):
>>         def g():
>>             seq = yield
>>             self.extend(seq)
>>             return self
>>         gtr = g()
>>         next(gtr)       # start it, so send method will work.
>>         return gtr
>>
>
> Replace these last three lines with...
>
>          return g()
>
>
> And the same for the rest of these.
>    Ron
>
>
>
>  And on a dictionary:
>>
>>      def __getter__(self):
>>          def g():
>>              seq = yield
>>              self.update(seq)
>>              return self
>>          getter = g()
>>          next(getter)
>>          return getter
>>
>>
>> on a string:    (bytes and tuples are very much like this.)
>>
>>      def __getter__(self):
>>           def g():
>>              seq = yield
>>              return self + seq
>>           getter = g()
>>           next(getter)
>>           return getter
>>
>> etc... It's pretty simple, but builtin versions of these would not need to
>> use the 'extend', 'update', or '__add__' methods, but can do the eqivalent
>> directly bypassing the method calls.
>>
>>
>> Then what you have is a input protocol that complements the iter output
>> protocol.
>>
>
> ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130718/89caeb3b/attachment.html>


More information about the Python-ideas mailing list