Package Products :: Package ZenUtils :: Module observable |
[hide private]
[frames] | no frames]

# Module observable

source code

    The observable module provides an interface and an optional mixin class that
    provide an Observer pattern for attribute based change notifications of an
    object.

    An object that is Observable (i.e. provides the IObservable interface) will
    allow attribute observers, a.k.a. listeners, to be attached or detached for
    specific attribute names. These observers will be notified whenever the
    specified attribute changes value.

    Observers can be any Python object that is callable.

    An example of using the observer pattern in your implementation is:

    class MyObject(MyParentClass, ObservableMixin):
        def __init__(self):
            super(MyObject, self).__init__()
            self.name = "Super Duper Object"
            self.age = 42

        def doIt(self):
            self.name = "I changed my name!"
            self.age = 37

    def ageListener(observable, attrName, oldValue, newValue):
        print "Age has been changed from %d to %d" % (oldValue, newValue)

    foo = MyObject()
    foo.attachAttributeObserver("age", ageListener)
    foo.doIt()

    This implementation will likely only work with new style classes that have been
    properly implemented.

Classes | [hide private]
IObservable Classes that implement the IObservable interface agree to provide the Observer pattern for object attribute changes.
ObservableMixin A mixin class that provides an implementation of the IObservable interface for any new-style class to use.