-1

What is @error_wrap in above of some functions in python?
such as below code:

@error_wrap
def disconnect(self):
    """
    disconnect a client.
    """
    logger.info("disconnecting snap7 client")
    return self.library.Cli_Disconnect(self.pointer)

error_wrap method:

def error_wrap(func):
    """Parses a s7 error code returned the decorated function."""
    def f(*args, **kw):
        code = func(*args, **kw)
        check_error(code, context="client")
    return f

I know about several OOP decorated python functions, (i.e. @staticmethod, @classmethod, @abstractmethod and etc), but I can't find about @error_wrap.
What is equivalent mentioned these codes?

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150

1 Answers1

1

Python decorators are also functions, but functions that receive as their first argument some other function. The code bellow is the same as yours

def error_wrap(f):
    # do something with received function f, then return it

def disconnect(self):
    # implementation of your function

disconnect= error_wrap(disconnect) # <- notice the function `disconnect` isn't called

And now you want to know what does error_wrap function do. Well we can't answer that because it's just another function so you should find it and paste her code, and then we'll answer your.

Daniel
  • 980
  • 9
  • 20
  • The `error_wrap` is a function you've pasted above. Decorator is just a name for a function that takes another function as its input and does something and then returns that function. As this was tagged as duplicate you can create another question where you ask what the code from `error_wrap` does. Cheers – Daniel Dec 11 '17 at 10:36