As part of improving the syntax for Aspect-Oriented Programming in Python based on the excellent answer by jsbueno: Aspect oriented programming (AOP) in Python , I was wondering if it's possible to accept a function template as a parameter in Python with any decoration.
Eg, being able to call foo(bar(5))
with bar
undefined, and have foo
receive an object representing the expression bar(5)
; rather than an error that bar
is undefined. (This would then be used as an input to construct a rule for matching incoming network messages indicating that another node has run function bar
with parameter 5.)
Is there any better way to do this than passing the template as a string and parsing it with ast
?
I am not exactly sure, what your looking for, maybe this will help:
def decorator(f):
def wrapper(*args, **kwargs):
print("f in wraper with: {}".format(f.__defaults__))
return f(*args, **kwargs)
return wrapper
funcs = {}
fdefaults = [1, 2, 3]
code = """def myfunc(x={}): return print("f returning:", *x)""".format(fdefaults)
exec(code, {}, funcs)
myfunc = decorator(funcs["myfunc"])
myfunc()
outputs: f in wraper with: ([1, 2, 3],) f returning: 1 2 3
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.