|
Treehopper Python API
|
A simple event handling class, which manages callbacks to be executed. More...
Public Member Functions | |
| def | __init__ (sender) |
| Construct a new EventHandle. More... | |
| def | __call__ () |
| Executes all callbacks. More... | |
| def | __iadd__ (callback) |
| Adds a callback to the EventHandler. More... | |
| def | __isub__ (callback) |
| Removes a callback from the EventHandler. More... | |
| def | __len__ () |
| Gets the amount of callbacks connected to the EventHandler. More... | |
| def | __getitem__ (index) |
| Gets the callback at the specified index. More... | |
| def | __setitem__ (index, value) |
| Sets the callback at the specified index. More... | |
| def | __delitem__ (index) |
| Deletes the callback at the specified index. More... | |
| def | add (callback) |
| Adds a callback to the EventHandler. More... | |
| def | remove (callback) |
| Removes a callback from the EventHandler. More... | |
Public Attributes | |
| callbacks | |
| a list of callbacks that have registered to this event More... | |
| sender | |
| the sender (captured by the constructor) More... | |
A simple event handling class, which manages callbacks to be executed.
This class uses C# syntax for subscribing/unsubscribing to events. You may either declare a plain function as a handler, or you can use a lambda.
Regardless, the first argument with which your method will be called is the "sender" of the event, which is always the class the EventHandler is instantiated inside (it's not the EventHandler object itself).
The second argument is the event. Unlike our strongly-typed EventArgs classes in C#, Java, and C++, this event class is more Pythonic: raw event values are published as the second argument of this event handler, removing the need to unpack the event.
Because this is not strongly typed, be sure to read the documentation so you know what type to expect.
Here's an example of subscribing to the Pin.analog_voltage_changed event using a lambda:
Though you could just as easily pass the name of a function in place of the lambda in-line statement. Imagine having a ``, then passing it to the event:
You can use this module in your own classes to publish events. Simply initialize a variable as an EventHandler:
Then call the event just like a function:
| def __init__ | ( | sender | ) |
Construct a new EventHandle.
| def __call__ | ( | ) |
Executes all callbacks.
Executes all connected callbacks in the order of addition, passing the sender of the EventHandler as first argument and the optional args as second, third, ... argument to them.
| def __delitem__ | ( | index | ) |
Deletes the callback at the specified index.
| def __getitem__ | ( | index | ) |
Gets the callback at the specified index.
| def __iadd__ | ( | callback | ) |
Adds a callback to the EventHandler.
| def __isub__ | ( | callback | ) |
Removes a callback from the EventHandler.
| def __len__ | ( | ) |
Gets the amount of callbacks connected to the EventHandler.
| def __setitem__ | ( | index, | |
| value | |||
| ) |
Sets the callback at the specified index.
| def add | ( | callback | ) |
Adds a callback to the EventHandler.
| def remove | ( | callback | ) |
Removes a callback from the EventHandler.
| callbacks |
a list of callbacks that have registered to this event
| sender |
the sender (captured by the constructor)