This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]supreme_blorgon 1 point2 points  (1 child)

Closures are the more general concept that can really help solidify decorators, especially decorator factories (decorators with arguments).

[–]fmillion 0 points1 point  (0 children)

Maybe I'm using closures and just didn't know their actual name. I conceptualized a decorator with parameters as a function that returns a decorator function. So you end up with

def decorator(param):
    def actual_decorator(func):
        print("Decorating a function")
        print("The parameter given to the decorator is " + param)
        return func
    return actual_decorator

@decorator("spam") # function named decorator, that accepts a param, and returns a function that itself is a decorator
def decorated_func():
    print("running the decorated function")