jfhbrook / pyee

A rough port of Node.js's EventEmitter to Python with a few tricks of its own
https://github.com/jfhbrook/public
MIT License
371 stars 39 forks source link

Preserve properties of decorated functions #21

Closed niklasf closed 9 years ago

niklasf commented 9 years ago

The properties (name, docstring, ...) of decorated functions should be preserved. Always return the original function from decorators.

jfhbrook commented 9 years ago

My python is really rusty. Why does this work?

niklasf commented 9 years ago

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.

jfhbrook commented 9 years ago

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?

niklasf commented 9 years ago

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(...).

jfhbrook commented 9 years ago

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?

niklasf commented 9 years ago

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.

jfhbrook commented 9 years ago

Thanks!

jfhbrook commented 9 years ago

Published as 1.0.1 https://pypi.python.org/pypi/pyee

niklasf commented 9 years ago

Thanks for the super fast release! I'll try to find a clean solution for removing once-listeners added by a decorator.