Why the code bellow doesn’t work for python3.10 when the @property decorator
is applied on the method already decorated with @property
.
# Define a decorator
def my_decorator(func):
def wrapper(*args, **kwargs):
print("Decorator applied")
return func(*args, **kwargs)
return wrapper
# Apply the decorator over the method
class MyClass:
@my_decorator
@property
def x(self):
"""This is the docstring of x"""
return 42
# Test the decorated method
obj = MyClass()
print(obj.x) # Output will be "Decorator applied" followed by "42"
print(obj.x.__doc__) # Output will be "This is the docstring of x"
For the first print it returns:
<bound method my_decorator.<locals>.wrapper of <__main__.MyClass object at 0x7e0b7943ebf0>>
And for the second print:
None
Everything worked fine with python3.8 except that I had to use functool.wraps
.
This is already a minimal example compared with my real scenario where my decorator is much more complex and also I have multiple decorators.