"indexed properties"...

pataphor pataphor at gmail.com
Thu May 22 08:58:41 EDT 2008


On Thu, 22 May 2008 06:26:41 -0500
David C. Ullrich <dullrich at sprynet.com> wrote:

> On Wed, 21 May 2008 12:47:44 +0200, pataphor <pataphor at gmail.com>
> wrote:
> >Using the trick of encapsulating the values inside single-element
> >lists one can make a transposition of the matrix and get
> >synchronicity for the little price of doubling the number of
> >instances. Since the views share the data this is a lot less
> >expensive than one would think. One can use the row view or the
> >column view to alter data and the changes will automatically be
> >visible in the other view, since the views update the same lists. 
> 
> Oh - that's different. This is not what I thought you had in mind
> (it's not what I had in mind in the thing I called a joke.)

The idea has two parts, the first part is to create two views and the
second part is to somehow synchronize the data. To make both views
update the same data is just not a very lazy synchronization
procedure. 

Does it even have to be a list? Let's see ...

No, this works as well:

class Shared(object):
    
    def __init__(self,value=None):
        self.value = value

class Storage(ListMixin):

  def __init__(self, seq=[]):
    self.L = map(Shared,seq)

  def _constructor(self, iterable):
    return Storage(iterable)

  def __len__(self):
    return len(self.L)

  def _get_element(self, i):
    assert 0 <= i < len(self)
    return self.L[i].value

  def _set_element(self, i, x):
    assert 0 <= i < len(self)
    self.L[i].value = x

  def _resize_region(self, start, end, new_size):
    assert 0 <= start <= len(self)
    assert 0 <= end   <= len(self)
    assert start <= end
    self.L[start:end] = [Shared() for i in range(new_size)]
 
> >  def _constructor(self, iterable):
> >    return Storage(iterable)
> 
> Probably I'm just being dense. Why does
> _constructor exist? (Oh - looking at things
> below, I imagine it's something to do with
> ListMixin.)

I just filled in the example class. It was very handy, one can make
changes very quickly because all the functionality is in a few lines of
code.

P.




More information about the Python-list mailing list