Python Decorator Help - Accessing variables within the decorator from within the decorating function

An alternative way to do decorators with a class and functools here, I find it a little bit easier to work with but in reality anytimes I touch decorators I need a small referesher lol- Script Benchmarking with Decorators Example - #4 by dkhayes117

If you note the first answer in the link @dkhayes117 returns the function result if there is one or None if there is an error and you can use your control flow like that.

I modified that a bit, I always return a Response object which is just

class Response:
    success: bool - true if no errors, false otherwise
    result: Any, whatever the result of the function was, None if success was False
    errorType:  str - python  or java to indicate what type of error
    errorMsg: the relevant error message (depends on if python or java)

So I decorate my most basic Business Logic with this, and then I can do control flow from that - was it successful - do X, was it a python error - I probably messed up code and need to tell them to alert me, if it's java, it may just be a expected database error and I can show a useful error to the end user etc. Seems like this may sort of be what you are trying to do, not sure your end goal though. Hope this is helpful.

1 Like