Skip to main content

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


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 details)

Uploaded Source

Built Distribution

observer_pattern-1.0.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file observer_pattern-1.0.0.tar.gz.

File metadata

  • Download URL: observer_pattern-1.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for observer_pattern-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c045e42434b0462507ef7000cf54c1cd2c2f02f02d804a59e6866d2fe54071ac
MD5 6fcb9ed54de2a1c53b381dbd15954149
BLAKE2b-256 ca83708013ca685377e3883ca521e221a54871aaa0403275017159548b4c2bf0

See more details on using hashes here.

File details

Details for the file observer_pattern-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for observer_pattern-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2903f0075dca46efd446f8996d78770d65966a6b1ab26a2f530c9c6fc5f715b9
MD5 12e8b0eaa2498f0ea370ca64649acf67
BLAKE2b-256 39affcf790b0200f3304d94e69952cd5c3a2ddb940c1cd2cf514e7a5d6426087

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page