decorator
decorator copied to clipboard
Making __name__ optional attribute of decorated-function if underlying function is missing it.
Hi,
I created a function A, using decorator.decorator
and applied it on another function B.
There were some more decorators (np.vectorize
here is the detail) applied on function B but decorator A was the top decorator.
I got error that function B
is missing __name__
attribute.
I was going through the decorator code and found this,
try:
fun.__defaults__ = func.__defaults__
except AttributeError:
pass
try:
fun.__kwdefaults__ = func.__kwdefaults__
except AttributeError:
pass
try:
fun.__annotations__ = func.__annotations__
except AttributeError:
pass
try:
fun.__module__ = func.__module__
except AttributeError:
pass
Can we also make __name__
as optional if underlying function misses this.
Yes, we can, I will add this feature to the next release of the decorator module.
BTW, you can probably solve your problem the decoration order: instead of
@your_decorator
@vectorize
def func():
pass
just write
@vectorize
@your_decorator
def func():
pass
@micheles Thanks for incorporating the changes. Could you let me know when are you planning to release it?
@vectorize
@your_decorator
def func():
pass
I actually wanted to update the output of np.vectorize
with the help of decorator
and that is why decorator
will be on the top.