Skip to main content

Observer pattern for functions and bound methods

Project description

observed allows you to to sign up functions or methods to “observe” other functions or methods:

from observed import event

def observed_func(arg):
    print("observed_func: %s"%(arg,))

def observer_func(arg):
    print("observer_func: %s"%(arg,))


>>> observed_func: banana
>>> observer_func: banana

You can also register observers for bound methods:

from observed import event

class Foo(object):
    def __init__(self, name): = name

    def bar(self, arg):
        print("Object %s invoked bar with arg='%s'"%(,arg))

def callback(arg):
    print("callback was invoked with arg='%s'"%(arg,))

a = Foo('a')
b = Foo('b')
# Sign up and callback to "observe"
# Now when we call, and callback will be invoked with
# the same arguments'baz')

>>> Object a invoked bar with arg='baz'
>>> Object b invoked bar with arg='baz'
>>> callback was invoked with arg='baz'

This example is included in ./observed/

You can ask that the observed object pass itself as the first argument whenever it calls observers:

from observed import event

def observed_func():
    print("observed_func: I was called")

def observer_func(observed):
    print("observer_func: %s called me"%(observed.__name__,))

observed_func.add_observer(observer_func, identify_observed=True)

>>> observed_func: I was called
>>> observer_func: observed_func called me

Notable features include:

  • A function or bound method is not kept alive just because it is observing something else. This is because the observed object does not keep any strong references to the observing objects.
  • The @event decorator can be used on methods in classes which are unhashable types, and can be used on an arbitrary number of methods in each class.
  • Tests included :)


observed exists on the python package index, so you can do pip install observed to install it. Alternatively, you can download the source distribution and in the root directory of the distribution do

$ python install.


See the file NEWS for the user-visible changes from previous releases.


observed is free (as in beer) software. See the LICENSE file.


observed can be obtained from the python package index

or via git


Basic usage is illustrated at the top of this file. Further examples are given in ./observed/

The source code is documented. Docstrings are required in contributions.


observed development is hosted on github. The current working repository is given in the Downloading section above.

Bug Reporting

Please submit bug tickets on the github tracking system

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date (8.1 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page