Is it possible to create a decorator which can be __init__‘d with a set of arguments, then later have methods called with other arguments?
For instance:
from foo import MyDecorator
bar = MyDecorator(debug=True)
@bar.myfunc(a=100)
def spam():
pass
@bar.myotherfunc(x=False)
def eggs():
pass
If this is possible, can you provide a working example?
Sure, a decorator is just a function which accepts a function and returns a function. There’s no reason that function can’t be (or, if you have arguments, can’t be returned by) an instance method. Here’s a really trivial example (because I’m not sure what exactly you’d be trying to do with this):
Like I said, I can’t think of a use case for this off the top of my head. But I’m sure they’re out there.