2021-01-02 15:58:24 +03:00
|
|
|
def decorator(func):
|
|
|
|
def wrapper(butts):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I am a great decorated function that now needs an argument!", butts)
|
2021-01-02 15:58:24 +03:00
|
|
|
func()
|
2021-01-04 17:33:43 +03:00
|
|
|
print("And this is after the function call.")
|
|
|
|
print("Decorating!")
|
2021-01-02 15:58:24 +03:00
|
|
|
return wrapper
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Decorator defined.")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
|
|
|
@decorator
|
|
|
|
def foo():
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Bar")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Function with decorator defined.")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
|
|
|
foo("I am the argument")
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Function with decorator called.")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
|
|
|
def __main__():
|
|
|
|
def decorator(func):
|
|
|
|
def wrapper(butts):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I am a great decorated function (within a function!) that now needs an argument!", butts)
|
2021-01-02 15:58:24 +03:00
|
|
|
func()
|
2021-01-04 17:33:43 +03:00
|
|
|
print("And this is after the function call (in a function call!)")
|
|
|
|
print("Decorating!")
|
2021-01-02 15:58:24 +03:00
|
|
|
return wrapper
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Decorator defined.")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
|
|
|
let i = 42
|
|
|
|
let l = [1,2,3]
|
|
|
|
|
|
|
|
@decorator
|
|
|
|
def foo():
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Bar (the inner one)")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Function with decorator defined.")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
|
|
|
foo("I am in the inner wrapper.")
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Function with decorator called.")
|
2021-01-02 15:58:24 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I like the numbers", l)
|
2021-01-02 15:58:24 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Calling main.")
|
2021-01-02 15:58:24 +03:00
|
|
|
__main__()
|
2021-01-03 02:31:28 +03:00
|
|
|
|
|
|
|
def methodDecorator(func):
|
|
|
|
def wrappedMethod(instance, butts):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I need an extra arg:", butts)
|
2021-01-03 02:31:28 +03:00
|
|
|
func(instance)
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Done.")
|
2021-01-03 02:31:28 +03:00
|
|
|
return wrappedMethod
|
|
|
|
|
|
|
|
class Foo():
|
|
|
|
def __init__(self):
|
|
|
|
self.foo = "bar"
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I am an initializer.")
|
2021-01-03 02:31:28 +03:00
|
|
|
def undecorated(self):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I am here.")
|
|
|
|
print("self.foo =", self.foo)
|
2021-01-03 02:31:28 +03:00
|
|
|
@methodDecorator
|
|
|
|
def decorated(self):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("(decorated) self.foo = ", self.foo)
|
2021-01-03 02:31:28 +03:00
|
|
|
|
|
|
|
let f = Foo()
|
|
|
|
f.undecorated()
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Returned from f.undecorated()")
|
2021-01-03 02:31:28 +03:00
|
|
|
f.decorated("butts")
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Returned from f.decorated()")
|
2021-01-03 02:31:28 +03:00
|
|
|
|
2021-01-03 03:29:09 +03:00
|
|
|
def decoratorWithArgument(decoratorArg):
|
|
|
|
def theActualDecorator(func):
|
2021-03-11 13:05:21 +03:00
|
|
|
print("I am decorating",func.__qualname__,"with this argument:",decoratorArg)
|
2021-01-03 03:29:09 +03:00
|
|
|
def wrapped():
|
2021-01-04 17:33:43 +03:00
|
|
|
print("This wrapper captured this decorator arg:", decoratorArg)
|
2021-01-03 03:29:09 +03:00
|
|
|
func()
|
|
|
|
return wrapped
|
|
|
|
return theActualDecorator
|
2021-01-03 02:31:28 +03:00
|
|
|
|
|
|
|
@decoratorWithArgument("foo")
|
|
|
|
def decoratedWithArgument():
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I don't take any args of mine own, and nor does my wrapper.")
|
2021-01-03 02:31:28 +03:00
|
|
|
|
|
|
|
decoratedWithArgument()
|
|
|
|
|
2021-01-03 03:29:09 +03:00
|
|
|
def methodDecoratorWithArguments(decoratorArgA, decoratorArgB):
|
|
|
|
def theActualDecorator(method):
|
2021-03-11 13:05:21 +03:00
|
|
|
print("I am decorating", method.__qualname__, "with these args:", decoratorArgA, decoratorArgB)
|
2021-01-03 03:29:09 +03:00
|
|
|
def wrapped(instance, wrapperArg):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I captured these from the decorator:", decoratorArgA, decoratorArgB)
|
|
|
|
print("I also take my own argument:", wrapperArg)
|
2021-01-03 03:29:09 +03:00
|
|
|
method(instance,"and modify this one")
|
|
|
|
return wrapped
|
|
|
|
return theActualDecorator
|
2021-01-03 02:31:28 +03:00
|
|
|
|
|
|
|
class Bar():
|
2021-01-04 15:49:23 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.foo = "weee"
|
|
|
|
|
2021-01-03 02:31:28 +03:00
|
|
|
@methodDecoratorWithArguments("foo","bar")
|
|
|
|
def superDecoratedMethod(self, arg):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I am method, so I can access myself", self.foo, "and also my own argument which is now", arg)
|
2021-01-03 02:31:28 +03:00
|
|
|
|
|
|
|
let b = Bar()
|
|
|
|
b.superDecoratedMethod("this arg goes to the wrapper")
|
2021-01-03 11:59:50 +03:00
|
|
|
|
|
|
|
def genericDecorator(func):
|
|
|
|
def wrapper(*args,**kwargs):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("I am a generic wrapper, I will pass all of my args along.")
|
2021-01-03 11:59:50 +03:00
|
|
|
func(*args,**kwargs)
|
2021-01-04 17:33:43 +03:00
|
|
|
print("And I am done.")
|
2021-01-03 11:59:50 +03:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
@genericDecorator
|
2021-01-09 07:58:46 +03:00
|
|
|
def aMethod(with_,args=None):
|
|
|
|
print("I got",with_,"and",args)
|
2021-01-03 11:59:50 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
aMethod()
|
2023-11-25 05:11:56 +03:00
|
|
|
except as exception:
|
2021-01-04 17:33:43 +03:00
|
|
|
print(exception.arg)
|
2021-01-03 11:59:50 +03:00
|
|
|
|
|
|
|
aMethod("just the first")
|
|
|
|
aMethod("the first","and the second")
|
2021-01-09 07:58:46 +03:00
|
|
|
aMethod(args="hello",with_="world")
|
2021-01-03 12:26:55 +03:00
|
|
|
try:
|
2021-01-09 07:58:46 +03:00
|
|
|
aMethod(foo="hello",with_="bar")
|
2023-11-25 05:11:56 +03:00
|
|
|
except as exception:
|
2021-01-04 17:33:43 +03:00
|
|
|
print(exception.arg) # unrecognized keyword arg, from inner method
|
2021-01-03 12:26:55 +03:00
|
|
|
|
|
|
|
aMethod(*[1,2])
|
|
|
|
try:
|
|
|
|
aMethod(*[1,2,3])
|
2023-11-25 05:11:56 +03:00
|
|
|
except as exception:
|
2021-01-04 17:33:43 +03:00
|
|
|
print(exception.arg) # too many arguments
|