Reputation: 2471
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
property
uses @functools.wraps()
property
is actually a descriptor
__get__
, but I can't figure out the syntaxExample 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?
Upvotes: 1
Views: 315
Reputation: 4060
You could flip the ordering of the decorators for the setter. Note I've adjusted the print_qualname_decorator
to call the underlying function and return it (otherwise the setter will not execute).
from functools import wraps
def print_qualname(func):
"""Wraps a method, printing its qualified name."""
@wraps(func)
def print_qualname_decorator(*args):
if hasattr(func, "__qualname__"):
print(f"Qualified name = {func.__qualname__}.")
else:
print("Doesn't have qualified name.")
return func(*args)
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
@some_attr.setter
@print_qualname
def some_attr(self, val: int) -> None:
self._some_attr = val
@print_qualname
def get_another_attr(self) -> int:
return self._another_attr
Use
In [46]: foo = SomeClass()
In [47]: foo.get_another_attr()
Qualified name = SomeClass.get_another_attr.
Out[47]: 0
In [48]: foo.some_attr = 5
Qualified name = SomeClass.some_attr.
In [49]: foo._some_attr
Out[49]: 5
Upvotes: 1