def _inner_onchange(func):
        "A decorator that runs a function only when a generic metric changes."
        def decorated(*args, **kwargs):
            try:
                mresult = metric(*args, **kwargs)
                if decorated._last_metric != mresult:
                    decorated._last_metric = mresult
                    decorated._last_result = func(*args, **kwargs)
            except AttributeError:
                decorated._last_metric = mresult    
                decorated._last_result = func(*args, **kwargs)

            return decorated._last_result
        return decorated
A decorator that runs a function only when a generic metric changes.
recipes/Python/426620_only_on_change_decorator/recipe-426620.py