Skip to main content

Bind functions to a value. When the value changes, the functions are called.

Project description

observable_variable

GitHub | PyPI

Do you want to call a function every time a variable gets a new value?

This package allows you to create an observable. This instance of the ObservableVariable class has an attribute value. If you set a new value to this attribute, every callback will be called. You can add callbacks by calling the method bind with the function and arguments to be passed to the callback. Removing is as easy as to call unbind with the same callback and arguments or by calling unbind_all to remove all bound callbacks for this observable. By default the new value will be the first argument to be passed to the callback. To disable this set the attribute send_value to False.

obs = ObservableVariable()



def my_callback(value, first_arg, second_arg):

    print("New value:", value)

    print("First argument:", first_arg)

    print("Second argument:", second_arg)



# binding a callback

obs.bind(my_callback, "arg 1", "arg 2")

obs.value = 1

>>> New value: 1

>>> First argument: arg 1

>>> Second argument: arg 2



# unbinding a specific function-args-combination

obs.unbind(my_callback, "arg 1", "arg 2")

obs.value = 2

# callback not called



obs.bind(lambda value: print(value))

# unbind all callbacks

obs.unbind_all()

obs.value = 3

# again callback not called



# deactivating the sending of the new value as the first argument

obs.send_value = False

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

observable_variable-0.0.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

observable_variable-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file observable_variable-0.0.2.tar.gz.

File metadata

  • Download URL: observable_variable-0.0.2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for observable_variable-0.0.2.tar.gz
Algorithm Hash digest
SHA256 400a64d51f7aa9fbfd085a7ca906c5478389014ab3f8e89afa9e3eb40b60f052
MD5 b637e965730f2ac5db0e4415cf18db73
BLAKE2b-256 c81370d4b24675a59e758338dda0c2ad3eebf9e8bd62950f8dd2e4c49a20191e

See more details on using hashes here.

File details

Details for the file observable_variable-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for observable_variable-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ac610de572bd501e8628feacee60c42a1ea90834c51982a5896b9802ad069c00
MD5 42c4f42452de46774c773f4ced037286
BLAKE2b-256 1798e13c6ab8dd56c33a61556e93f665f8a00ce0dbf5af74610a084c7bb76bac

See more details on using hashes here.

Supported by

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