Transcrypt
Transcrypt copied to clipboard
Is there any elegant way to preserve decorated function name ?
Without surprise,
@mydecorator
def myfunction():
...
is transpiled to
var myfunction = mydecorator(myfunction(){
....
As a result myfunction.name
is "mydecorator"
. This is a problem for me because I rely on function names in the rest of my application. This is a classic problem and python provides utilities for that like the functools.wraps
decorator (link) but is there a similar utility in transcrypt allowing that ? A pragma maybe ?
The only trick I found is to add
__pragma__("js", {},'Object.defineProperty(myfunction, "name", { value: "myfunction" });')
after the declaration of my function (func.name being read-only in js, that's the trick I have found) but it's rather inelegant.
Perhaps this is a nice "growth kernel" of a future functools
module,
having a simple form of update_wrapped
and wraps
, that for a start only deals with the name
,
since that seems to be the most useful attribute.
A first version of update_wrapped
wouldn't have params except for wrapper and wrapped.
I had a need for this same thing. I worked around it for now by using adding an argument to my decorator that lets me explicitly pass in the function name. But having the original function name available inside the decorator as __name__
would have made for a bit cleaner code.