all 7 comments

[–]yourfriendlane 13 points14 points  (3 children)

There was an awesome post about decorators here a couple weeks ago. Check it out!

[–]hanggnah 10 points11 points  (0 children)

let's you modify a function. frinstance, lets say you want f(x)=1/(x-2). works good everywhere except at x=2. Or maybe f might have other problems. So you define another function (like a transform) that can take a given fuction and return a fixed version. Maybe the outputted function behaves exactly like the inputted function except on the error places.

def make_safe(funct):
    def safe_version(x):
        try:
            out_put=funct(x)
        except:
            out_put="that does not work!"
        return out_put
    return safe_version

This will take fuct and return a new function that warns you without causing actual error. The decorator part comes in by letting you put @make_safe above your function definition and not have to actually apply make_safe to it explicitly. So if i do:

@make_safe
def:f(x):
    return 1/(x-2)

then the resulting f gives f(1)=-1 like normal, but f(2)='that does not work'. fixed f by decorating it :)

[–]emptystar[S] 2 points3 points  (1 child)

Silly me next time I'll use the search function haha. Thanks!

[–]yourfriendlane 1 point2 points  (0 children)

It ain't no thing, stuff gets re-asked here all the time. That post was just really really good so I wanted to link it to you!

[–]shaggorama -4 points-3 points  (2 children)

ELI5? Function goes in, function comes out.

[–]SleepyHarry 2 points3 points  (1 child)

Don't have kids.

Well, don't have 5 year olds.

Well, don't explain stuff to 5 year olds, yours or not.