1

I need advice from experience programmers as I am failing to wrap my head around this. I have the following data structure.

class obj(object):
    def __init__(self, data=[], meta=[]):
        self.data=data
        self.meta=meta

class subobj(object):
    def __init__(self, data=[]):
        self.data=data

Say, I'm creating the following objects from it.

sub1=subobj([0,1,5])
sub2=subobj([0,1,6])
sub3=subobj([0,1,7])
objA=obj(data=[sub1,sub2], meta=[3,3])
objB=obj(data=[sub3,sub1], meta=[3,3])

Now I am changing sub1 operating on the second object as well as its metadata. For simplicity, I'm writing here via obj. vars without setter/getters:

objB.data[1].data+=[10,11]
objB.meta[1]=5

Now, objA.data[0] has (obviously) changed. But objA.meta[0] stayed the same. I want some func(objB.meta[1]) to be triggered right after the change of the value in objA.data (caused in objB.data) and to change objA.meta as well. Important: this func() uses metadata of the changed sub1 from objB.

I simply don't know how to make every obj know about all other objs that share the same subobj as it does. I could make a func() be triggered upon having that knowledge. I would appreciate any hints.

Notes:

  1. I want to pass those subobj around between objs without metadata and let them be changed by those objs. Metadata is supposed to store information that is defined within objs, not subobj. Hence, the value of the func() depends on the obj itself, but its definition is the same for all objs of the class obj.

  2. For simplicity, this func(metadata) can be something like multiply3(metadata).

  3. I will have thousands of those objects, so I am looking for rather an abstract solution that is not constrained by a small number of objects.

Is that possible in the current design? I am lost as to how to implement this.

9
  • Um, are your subobjects going to have additional properties on them other than data? In your example, you modify the .els property of a subobject. Commented Oct 23, 2015 at 18:26
  • If you want to have a parent object be notified when a child object's property changes, you can use events, and have the parent subscribe to its children's events in the constructor. Have a look at python-utilities.readthedocs.org/en/latest/events.html to see if it meets your needs. Commented Oct 23, 2015 at 18:34
  • Sorry. I was just rewriting in a more clear way. It simply is objB.data[1].data+=[10,11]. subobjs are simply meant to hold data that is updated by different "parent" objects. But they are objs themselves. Commented Oct 23, 2015 at 18:35
  • Do you share the same value in two different subobject.data properties? Would a tuple instead of a list do? Also, please include @ppperry in your comments when replying to me on your post. Commented Oct 23, 2015 at 18:40
  • @ppperry A tuple wouldn't do since I will be changing .data and want the changes to be propagated to all objs that hold the subobj. Commented Oct 23, 2015 at 18:42

1 Answer 1

1

Assuming that objs' data property can only contain subobjs and can never change, this code should work.

class obj(object):
    __slots__=("data","meta")
    def __init__(self,data=(),meta=None):
        meta = meta or []
        self.data = data
        self.meta = meta
        for so in data:
            so._objs_using.append(self)
class subobj(object):
    __slots__ = ("_data","_objs_using")
    def __init__(self,data=()):
        self._objs_using=[]
        self._data = data
    @property
    def data(self):
        return self._data
    @data.setter
    def data(self,val):
        self._data = val
        for obj in self._objs_using:
            metadata_changed(obj.meta)

I called the function that you want to call on the metadata metadata_changed. This works by keeping track of a list of objs each subobj is used by, and then creating a special data property that notifies each obj whenever it changes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.