Bind functions to a value. When the value changes, the functions are called.
Project description
observable_variable
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
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
Built Distribution
Hashes for observable_variable-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d1244823c5358c3672d20964d368c19e27048a8e1412b0fa9ca8a2585cfd3d7 |
|
MD5 | 733109254bf09dbcd2e8333885e6de18 |
|
BLAKE2b-256 | 51e4c1ed021cd562732e7987f67d7205a0c59126e63702821c3230784b573eab |
Hashes for observable_variable-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 325e0fde670d3b1b502c1ceaff4f8b2c155e3794f04801a4b7253347ee5b2018 |
|
MD5 | 21bc66c6074a5edd5a6ac0d3115193c3 |
|
BLAKE2b-256 | 3f0a1926ef6f45b637dbf2f2d7f60a6a56695423e0b8fb9a2370cbe71f837a5d |