python - Import path for decorator-wrapped function? -
assuming have decorator , wrapped function this:
def squared(method): def wrapper(x, y): return method(x*x, y*y) return wrapper @squared def sum(x, y): return x+y
i have other code call undecorated version of sum
function. there import trick can me unwrapped method? if code says from some.module.path import sum
then, wrapped version of sum
method, not want in case. (yes, know break out helper method, breaks of cleanliness of pattern i'm going here.)
i'm okay adding "magic" decorator provide alternate symbol name (like orig_sum
) import, don't know how that.
def unwrap(fn): return fn.__wrapped__ def squared(method): def wrapper(x, y): return method(x*x, y*y) wrapper.__wrapped__ = method return wrapper @squared def sum(x, y): return x+y sum(2,3) -> 13 unwrap(sum)(2,3) -> 5
Comments
Post a Comment