[Python-ideas] Adding __getter__ to compliment __iter__.
Ron Adam
ron3200 at gmail.com
Thu Jul 18 15:46:12 CEST 2013
On 07/18/2013 01:51 AM, Clay Sweetser wrote:
> What is the difference between this "getter protocol" and using a
> generator's send method?
A protocol specifies how something is done and what's required to do it.
A generators send method is just a send method on any generator.
The Protocol in this case, is each iterable object has a __getter__ method
that uses its' send() method to receive an iterable object for the purposes
of extending the instance. (or returning a new instance if it's a mutable
object.)
Cheers,
Ron
> On Jul 18, 2013 2:47 AM, "Ron Adam"
> <ron3200 at gmail.com
> <mailto: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
> <mailto:Python-ideas at python.org>
> http://mail.python.org/__mailman/listinfo/python-ideas
> <http://mail.python.org/mailman/listinfo/python-ideas>
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
More information about the Python-ideas
mailing list