简体   繁体   中英

Functions decorator safety in python

How this code works? It is from chapter 8 in "Data Science by Scratch" from the gradient. Why do I need to wrap a function inside another fucntion? Is there a more readable way to achieve this execption handling? Here's the explanation for it.

It is possible that certain step sizes will result in invalid inputs for our function. So we'll need to create a “safe apply” function that returns infinity (which should never be the minimum of anything) for invalid inputs:

def safe(f):
    """return a new function that's the same as f,
    except that it outputs infinity whenever f produces an error"""
    def safe_f(*args, **kwargs):
        try:
             return f(*args, **kwargs)
        except:
             return float('inf')
    return safe_f

You are missing the function call:

Lets say I define an average function like this:

def naive_average(lst):
    return float(sum(lst))/len(lst)

What would happen if lst is empty ? or if it contains something that isn't numeric ? BOOM, exception !

With the decorator you mentioned, the function would look like this

@safe
def naive_average(lst):
    return float(sum(lst))/len(lst)

and now, calling it on an empty lst would return float('inf') instead of an exception

Lets say we have a trivial function like this:

def myfunc(n):
    return 42/n

and we do this:

print(myfunc(0))

We get this:

Traceback (most recent call last):
  File "gash.py", line 14, in <module>
    print(myfunc(0))
  File "gash.py", line 12, in myfunc
    return 42/n
ZeroDivisionError: division by zero

Now we do this:

myfunc = safe(myfunc)
print(myfunc(0))

We get this:

inf

The second time we call safe() which returns a new function with the embedded exception handling. We replace what the name "myfunc" refers to, now it refers to the returned function. The original myfunc is not lost, it is called f inside the new one.

A @safe decorator is essentially doing the same thing as myfunc = safe(myfunc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM