Simple Observer Pattern implementation
Project description
observer-pattern
Simple Observer Pattern Implementation
from observer_pattern import Observable
counter = 0
def plus_n(n):
global counter
counter += n
def plus_2n(n):
global counter
counter += 2*n
def plus_3n(n):
global counter
counter += 3*n
observable = Observable()
observable.subscribe(plus_n)
observable.subscribe(plus_2n)
observable.subscribe(plus_3n)
observable.notify(2)
observable.unsubscribe(plus_3n)
observable.notify(5)
print(counter) # 27
print(len(observable)) # 2
You can pass any number of args
and kwargs
def print_product(a, b):
print(a * b)
observable = Observable()
observable.subscribe(print_product)
observable.notify(3, b=5) # 15
print(print_product in observable) # True
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
observer_pattern-1.0.0.tar.gz
(14.2 kB
view hashes)
Built Distribution
Close
Hashes for observer_pattern-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2903f0075dca46efd446f8996d78770d65966a6b1ab26a2f530c9c6fc5f715b9 |
|
MD5 | 12e8b0eaa2498f0ea370ca64649acf67 |
|
BLAKE2b-256 | 39affcf790b0200f3304d94e69952cd5c3a2ddb940c1cd2cf514e7a5d6426087 |