python - Access a parent class' property getter from the child class -
i'm attempting set test caching tool in cache based on function object , arguments. test, need access exact function being called in property, , can't figure out how this.
the caching code looks this:
def cached(func): """ decorator cache function calls """ @wraps(func) def wrapper(self, *args): # cache lives in instance gets garbage collected if (func, args) not in self._cache: self._cache[(func, args)] = func(self, *args) return self._cache[(func, args)] return wrapper and have cached property:
class spatialmixin(object): @property @cached def longitude_extrema(self): return 25 the class looks this:
class mainclass(someotherbaseclass,spatialmixin): pass myobject = mainclass() i can access base_class.spatialmixin.longitude_extrema.fget directly, different object myobject.longitude_extrema.fget (which i'm using indicate getter of property; can't access way because myobject.longitude_extrema number 25).
so, how can access function underlying myobject.longitude_extrema property?
if you're trying access original longitude_extrema function, you're not finding through fget because original longitude_extrema function isn't getter. wrapper function created in cached decorator getter.
on python 3.4+, wrapper decorated functools.wraps has __wrapped__ attribute pointing function wraps. can use access original longitude_extrema. (this attribute exists on 3.2 , 3.3, buggy behavior.)
on python 2, functools.wraps doesn't set __wrapped__. while it's technically possible @ wrapped function anyway direct closure manipulation, it's awkward, variable-name-dependent, , not option, since control decorator. set __wrapped__ in cached decorator.
Comments
Post a Comment