Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47597

Re: Newbie: question regarding references and class relationships

References <kp4jf4$5fu$1@dont-email.me> <roy-FBA6CB.09354310062013@news.panix.com> <kp4lnn$hfi$1@dont-email.me>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-06-10 13:56 -0600
Subject Re: Newbie: question regarding references and class relationships
Newsgroups comp.lang.python
Message-ID <mailman.2985.1370894224.3114.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jun 10, 2013 at 7:57 AM, Rui Maciel <rui.maciel@gmail.com> wrote:
> # Case A: this works
> model.points[0].position = [2,3,4]
> line.points
>
> # Case B: this doesn't work
> test.model.points[0] = test.Point(5,4,7)
> line.points
> </code>
>
>
> Is there a Python way of getting the same effect with Case B?


It's informative to think about what object is actually being mutated
in each of those examples.  In A, you are mutating the Point instance
by replacing its "position" attribute with a new list.  Since the Line
object references the same Point instance, it "sees" the change.  In
B, you are actually mutating a list *containing* the Point instance,
by replacing the Point in the list with some new Point.  The replaced
Point itself is not changed at all (apart from having one fewer
reference), and so from the perspective of the Line nothing has
changed.

There are a couple of ways you might get this to work the way you
want.  One is by adding as an extra layer a proxy object, which could
be as simple as:

class Proxy(object):
    def __init__(self, ref):
        self.ref = ref

Instead of adding points to the model, add instances of Proxy that
contain the points.  When adding points to the lines, add the same
Proxy objects instead.  Later, when you want to replace a point in the
model, leave the Proxy in place but change the Point it contains by
modifying the "ref" attribute.  Since the Line only directly
references the Proxy, it must follow the same ref attribute to access
the Point, and so in that way it will "see" the change.  The downsides
to this approach are that you have to then use the Proxy objects all
over the place and explicitly "dereference" them by using the ref
attribute all over the place.

Another possibility is to subclass the list used to store the points,
in order to modify the way that it replaces items.  That could look
something like this:

class UpdateList(list):
    def __setitem__(self, index, item):
        if isinstance(index, slice):
            indices = range(* index.indices(len(self)))
            items = list(item)
            if len(indices) != len(items):
                raise TypeError("Slice length does not match sequence length")
            for i, x in zip(indices, items):
                self[i].update(x)
        else:
            self[index].update(item)

Then you just need to add an "update" method to the Point class (and
any other class you might want to use this with) that would update the
Point's attributes to match those of another point.  The downside here
is that this is not very Pythonic, in that somebody familiar with
Python who is reading the code would be surprised by the way the model
updates work.

A third possibility of course would be to just not try to do case B.

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:18 +0100
  Re: Newbie: question regarding references and class relationships Roy Smith <roy@panix.com> - 2013-06-10 09:35 -0400
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:57 +0100
      Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 15:00 +0100
      Re: Newbie: question regarding references and class relationships Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-10 13:56 -0600
        Re: Newbie: question regarding references and class relationships Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-11 17:02 -0700
  Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 15:49 +0200
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 14:59 +0100
      Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 16:28 +0200
        Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 15:38 +0100
          Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 17:05 +0200
            Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 16:20 +0100
              Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 17:55 +0200
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 17:09 +0100
                Re: Newbie: question regarding references and class relationships Peter Otten <__peter__@web.de> - 2013-06-10 19:07 +0200
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 18:42 +0100
                Re: Newbie: question regarding references and class relationships Dave Angel <davea@davea.name> - 2013-06-10 15:36 -0400
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:31 +0100
                Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 15:51 -0400
                Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:13 +0100
                Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 18:07 -0400
                Re: Newbie: question regarding references and class relationships Grant Edwards <invalid@invalid.invalid> - 2013-06-10 22:39 +0000
                Re: Newbie: question regarding references and class relationships Chris Angelico <rosuav@gmail.com> - 2013-06-11 08:54 +1000
                Re: Newbie: question regarding references and class relationships Tim Chase <python.list@tim.thechases.com> - 2013-06-10 18:24 -0500
                Re: Newbie: question regarding references and class relationships Dave Angel <davea@davea.name> - 2013-06-10 19:26 -0400
                Re: Newbie: question regarding references and class relationships Jason Swails <jason.swails@gmail.com> - 2013-06-10 23:49 -0400
  Re: Newbie: question regarding references and class relationships Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-10 15:54 -0400
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-10 21:09 +0100
  Re: Newbie: question regarding references and class relationships Rick Johnson <rantingrickjohnson@gmail.com> - 2013-06-11 16:50 -0700
    Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-13 13:06 +0100
      Re: Newbie: question regarding references and class relationships Chris Angelico <rosuav@gmail.com> - 2013-06-14 00:07 +1000
        Re: Newbie: question regarding references and class relationships Rui Maciel <rui.maciel@gmail.com> - 2013-06-13 19:23 +0100

csiph-web