Is it possible to access function attributes inside a decorator? Consider below piece of code.
def deco(a):
def wrap():
print(a.status)
a()
print(a.status)
return wrap
@deco
def fun1():
fun1.status="bar"
fun1.status="foo"
fun1()
I expected the output to be :
foo
bar
But I get the below error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
fun1()
File "D:\python_projects\test_suite\func_attribute.py", line 3, in wrap
print(a.status)
AttributeError: 'function' object has no attribute 'status'
Is there any way to make this work since
def fun1():
fun1.status="bar"
fun1.status="foo"
a=fun1
print(a.status)
a()
print(a.status)
Outputs:
foo
bar
As expected.
Thanks to the decorator, the global name
fun1is bound to the decoration result, so to the nestedwrap()function object. Insidewrap()however,arefers to the original, unwrapped function object.So you have two different function objects, and each can have attributes; they are not the same objects.
fun1.statusis a different attribute froma.status.You can access the same object as
fun1in the decorator aswrap:Demo: