Skip to main content

Observer pattern made muy facil

Project description


Build Status

Python evento package, making the Observer pattern estúpida sencillo.


pip install evento


from evento import Event

# observers are simply methods
def observer(param1, param2):
	print(', '.join([param1, param2]))

# we need to create an instance for every event we want to fire
demo_event = Event()

# subscribe observer to the event
demo_event += observer

# trigger notifications for the event (run all observers)
demo_event('Hello', 'World') # => "Hello, World"

Usage within classes

This is how Events are typically used to decouple code into separate classes;

from evento import Event

class Action:
	def __init__(self, name): = name
		self.runEvent = Event()

	def run(self):
		# do action-specific stuff

class ActionCounter:
	def __init__(self, action_performer):
		self.count = 0
		self.actionEvent = action_performer.runEvent
		self.actionEvent += self._onActionRun

	def __del__(self):
		# observers should always make sure to _unsubscribe_ from events when they are done
		self.actionEvent -= self._onActionRun

	def _onActionRun(self, action_performer):
		self.count += 1
		print("'{0}' ran {1} times".format(, self.count))

performer = Action('Foo action')
observer = ActionCounter(performer) # => "'Foo action' ran 1 times" # => "'Foo action' ran 2 times"

Unsubscribe function returned by .add

# setup
event = Event()

def setup():
	def handler(value):

	unsubscribe = event.add(handler)
	return unsubscribe

cleanup = setup()

# do stuff
# ...

# cleanup


The evento package provides the following decorators to easily add before and/or after events to any method:


They can be used as followed:

from evento import triggers_before_event, triggers_after_event, triggers_beforeafter_events

def before(event):

def after(event):

def before_action(param1):

def after_action(param1, param2, param3):
    print(' '.join([param1, param2, param3]))

def both_action():

before_action('first before') # => "first before"
after_action('first after:', '1', '2') # => "first after: 1 2"
both_action() # => "during"

# subscribe callbacks to the decorated methods
both_action.subscribe(before, after)

before_action('second before') # => "before\nsecond before"
after_action('second after:', '3', '4') # => second after: 3 4\nafter"
both_action() # before\nduring\nafter

Note that these decorators simply wrap the function in a class that also holds a beforeEvent and/or afterEvent, which is/are triggered before/after the function is invoked. To clarify; within the context of the above example, the following two lines do exactly the same:

before_action.beforeEvent += before

Project details

Download files

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

Files for evento, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size evento-1.0.2-py3-none-any.whl (4.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size evento-1.0.2.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page