[Python-ideas] Adding __getter__ to compliment __iter__.

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jul 18 17:06:44 CEST 2013


On 18 July 2013 10:08, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
> Then builtin sum() would have the following case added to handle the
> builder protocol:
>
>     try:
>         bldr = builder(start)
>     except TypeError:
>         pass
>     else:
>         for item in iterable:
>             bldr += item
>         return bldr.finish()

What use cases would the builder protocol have apart from using sum
with collections (since that particular case is already well-covered
by chain/join)?

Wouldn't it be easier to put that logic into the constructor for
type(collection) or into a factory function. Then you wouldn't need an
additional protocol or an additional class (for each buildable
collection).

Why would you want to do this

    bldr = builder(()) # Build a tuple
    for val in stuff:
        bldr += item  # Or append/extend
    result = bldr.finish()

when you can just do this

    result = tuple(chain(stuff)) # or tuple(stuff)

Most non-string collections already support this interface in their
constructors or in a factory function.


Oscar


More information about the Python-ideas mailing list