[Python-ideas] Extending properties to sequence-like behavior

Nick Coghlan ncoghlan at gmail.com
Thu Apr 7 06:36:33 CEST 2011


On Thu, Apr 7, 2011 at 2:03 PM, Dan Baker <dbaker3448 at gmail.com> wrote:
>> Perhaps rename the returned class to SequenceProp and have it inherit
>> from collections.MutableSequence?
>
> I hadn't really looked around at the collections module before. If I'm
> understanding it correctly, if I implement a few of the basic methods
> for MutableSequence (__getitem__, __setitem__, __delitem__, and
> insert) I get a few of the extra list-like methods (append, extend,
> count, index, pop, etc.) and the iterator protocol for free. If that's
> the case, that sounds like a huge win. Probably wouldn't even need to
> do __call__ for the GetAll method anymore in most cases, I'd be able
> to just use "for item in box.items" and the __iter__ method handles
> it.
>
> Thanks for the idea. That looks like it would make access via the
> sequence property much more natural.

Yeah, the collections ABCs were inspired in part by the old UserDict
and UserList classes - make it easier to support the broader APIs by
implementing a few essential methods. Being able to do that is one of
the big reasons Guido opted for standard library level ABC support
over Java-style interface definitions.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list