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.1.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

observable_variable-0.0.1-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

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