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

Nick Coghlan ncoghlan at gmail.com
Wed Apr 6 15:49:08 CEST 2011


On Wed, Apr 6, 2011 at 3:31 PM, Dan Baker <dbaker3448 at gmail.com> wrote:
> I've attached some scratch code here (Python 2.7) demonstrating the
> basic idea. Is there a better way to do this?

Perhaps rename the returned class to SequenceProp and have it inherit
from collections.MutableSequence?

Then you could override methods and use GetAllItems/SetAllItems at
appropriate points (e.g. in __iter__, or when the item access methods
are passed slices with start, stop and step all set to None).

It will be a little more work up front, but it's the only way you're
going to get full control over the way client code interacts with your
external sequence objects.

One of the problems you have at the moment is that other mutating
methods of lists (such as .sort() and .reverse()) still aren't going
to work as would be expected if "box.Items" truly was a list. Avoiding
that claim and instead making it clear you only provide the smaller
sequence ABC would help make that clear (and MutableSequence
automatically defines many of those operations in terms of the core
abstract methods, anyway).


Cheers,
Nick.

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



More information about the Python-ideas mailing list