[Python-ideas] Adding __getter__ to compliment __iter__.

Ron Adam ron3200 at gmail.com
Thu Jul 18 08:46:46 CEST 2013



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.



More information about the Python-ideas mailing list