Event based or reactive programming
- Last edited on Jul 8, 2015, 7:54:09 PM by davidism
a is the result of some expression that references value
b is updated, you want
a to be recalculated. This concept of propagating changes is called reactive programming.
b = 1 a = 2 * b b = 2 assert a == 4
It is often seen in GUI libraries, where a change to one control automatically updates some other part of the window.
This is not a commonly used pattern in Python. While it is good at introspecting code and the call stack, it is not trivial to track all the dependencies in a dynamic language. It’s also not possible to override the built-in types and functions, so any reactive pattern for Python needs to rely on special wrappers.
Some libraries that provide a “reactive” pattern are:
- Traits: provides its own types rather than built-in Python types
- Trellis: very old, and lots of black magic going on behind the scenes. May not work with newer versions of Python. Probably the closest thing to a pure reactive pattern to be found.
It is more common to use a signalling library instead. Register callbacks on events, then send these events from other parts of the code. Many gui toolkits provide their own way to send and listen to events. There are many standalone packages as well.
- Blinker: recommended in the Flask docs