Object instance "reporting" to a container class instance

Daniel Lipovetsky daniel.lipovetsky at gmail.com
Sun Mar 11 20:36:22 EDT 2007


I would like for an object to "report" to a container object when a
new instance is created or deleted. I could have a container object
that is called when a new instance is created, as below.

class AnyObject:
    pass

class Container:
    links = []
    def add(self,other):
        while other not in self.links:
            self.links.append(other)
    def rem(self,other):
        while other in self.links:
            self.links.remove(other)
...

container = Container()
a = AnyObject()
container.add(a)

My question is: can (should? :-) this "reporting" be done inside the
instance's __init__ and __del__ methods (that is, an instance
"reports" to the container as soon as it is created or deleted)?

Thanks!
Daniel

---

I'm working out a design where Object A is "linked" to Object B, and
both objects become aware of that relationship. I have implemented an
example successfully; the code is below. My main question is above,
but I would appreciate comments on the code! (For example, I'm
wondering whether my way of defining variables in the class but
assigning them locally to each instance (in the "Object.init" method)
is really a bad kludge...)


class Object():
    def __del__(self):
        print "buh-bye!", self # Verbose for understanding garbage cleanup
    def init(self,name):
        self.links = []
        self.name = name
    def add(self,other):
        while other not in self.links:
            self.links.append(other)
            other.add(self)
    def rem(self,other):
        while other in self.links:
            self.links.remove(other)
            other.rem(self)

class Student(Object):
    def __init__(self,name):
        self.init(name)

class Section(Object):
    def __init__(self,name):
        self.init(name)

class Task(Object):
    def __init__(self,name):
        self.init(name)

## Construct test instances!

students = {}
for name in ['Jose','Daniel','Rusty']:
    student = Student(name)
    students[name] = student

sections = {}
for name in ['English 1']:
    section = Section(name)
    sections[name] = section

tasks = {}
for name in ['Homework 1','Exam 1','Homework 2','Exam 2']:
    task = Task(name)
    tasks[name] = task

# Display example connections
def show_connections():
    for section in sections:
        print sections[section].name
        for link in sections[section].links:
            print "\t", link.name

# Add some connections...

print "Now adding connections..."
for name in tasks:
    sections['English 1'].add(tasks[name])
    show_connections()

# Remove some connections...

print "Now removing connections..."
for name in tasks:
    sections['English 1'].rem(tasks[name])
    show_connections()

for task in tasks:
	print tasks[task].links

for section in sections:
	print sections[section].links

## Test garbage cleanup

sections['English 1'].add(tasks['Exam 1'])
print sections['English 1'].links
sections['English 1'].rem(tasks['Exam 1'])
del sections['English 1']



More information about the Python-list mailing list