Closed niklasf closed 9 years ago
My python is really rusty. Why does this work?
How does once
work now? Instead of registering f
for the event, _once(f)
gets registered to execute f
once and then remove the event listener. What will the old lambda return? _once(f)
.
The patch still does exactly the same, but returns the original f
instead.
So for instance, always_event_handler
is defined to be what's returned by the decorator, and not what's decorated itself? Meaning that now when we reference always_event_handler
later on it retains the original function's docstrings?
Exactly. (Precisely because it still is the original function, because we return it from the decorator.)
Note that this was already working for on(...)
. I just added test for it and adjusted once(...)
.
Also, as a bonus: I say in the README
**ee.remove_listener(event, fxn)**: Removes the function ``fxn`` from ``event``.
Requires that the function is not closed over by ``ee.on`` (using this with the
decorator style is unfortunately not possible).
That's no longer true, is it?
Yeah. It wasn't even true before this patch. See this already exisiting comment that says exactly the opposite: https://github.com/jfhbrook/pyee/blob/master/pyee/__init__.py#L88
It is still true for once()
. We now retain the original function, but some anonymous _once(f)
is added as the event listener and we have no other reference to that.
Thanks!
Published as 1.0.1 https://pypi.python.org/pypi/pyee
Thanks for the super fast release! I'll try to find a clean solution for removing once-listeners added by a decorator.
The properties (name, docstring, ...) of decorated functions should be preserved. Always return the original function from decorators.