I have an instance attribute that I made a property using Python's property
decorator.
I then made a setter for the property using the decorator @property_name.setter
.
How can I get the __qualname__
of the original method definition, decorated with @property.setter
?
Where I Have Looked
- Python: __qualname__ of function with decorator
- I don't think
property
uses@functools.wraps()
- I don't think
- Python @property.setter
- I realize
property
is actually adescriptor
- I realize
- Decorating a class method after @property
- Tells me I may want to use
__get__
, but I can't figure out the syntax
- Tells me I may want to use
Example Code
This was written in Python 3.6
.
#!/usr/bin/env python3
def print_qualname():
"""Wraps a method, printing its qualified name."""
def print_qualname_decorator(func):
# print(f"func = {func} and dir(): {dir(func)}")
if hasattr(func, "__qualname__"):
print(f"Qualified name = {func.__qualname__}.")
else:
print("Doesn't have qualified name.")
return print_qualname_decorator
class SomeClass:
def __init__(self):
self._some_attr = 0
self._another_attr = 0
@property
def some_attr(self) -> int:
return self._some_attr
@print_qualname()
@some_attr.setter
def some_attr(self, val: int) -> None:
self._some_attr = val
@print_qualname()
def get_another_attr(self) -> int:
return self._another_attr
Output:
Doesn't have qualified name.
Qualified name = SomeClass.get_another_attr.
How can I get the __qualname__
for some_attr
from inside the print_qualname
decorator? In other words, how do I get SomeClass.some_attr
to be output?