python 用函数包装函数
python wrap function with function
我有下面这两个功能。我想先 运行 验证然后 child 但我想用验证装饰 child 所以我可以告诉它 运行 先验证给定的输入然后传递输出到 child 到 运行 就可以了。
def validate(x, y):
print(x, y)
x = x+2
y = y +1
return x, y
def child(x, y):
print(x)
print(y)
return x, y
我该怎么做?
显然,这是行不通的:
def validate(x):
print(x)
x = x+2
return x
@validate
def child(x):
print(x)
return x
我想用装饰器的方式实现这样的目标:
child(validate(2))
编辑:
我有一些方法“data_parsing
”接受输入并对输入的数据进行一些登录。数据可能出现故障,所以我创建了一个 class 方法,首先验证输入的数据。如果数据格式不正确,我会实例化 class 和 运行 验证,首先引发异常。如果成功,我将进入下一个函数调用 data_parsing
() ,该函数调用获取数据并对其进行处理。所以逻辑是:
def execute(data):
validator_object(data).run()
data_parsing(data)
编辑:
def validator(fnc):
def inner(*aaa):
a,b = aaa
a += 4
return fnc(a,b)
return inner
@validator
def child(*aaa):
a,b = aaa
print(a)
return a
a = 1
b = 2
child(a, b)
请注意,@decorator
形式应用于函数声明阶段,它会立即包装目标函数。
您可以针对您的案例使用以下实现:
def validate(f):
@functools.wraps(f)
def decor(*args, **kwargs):
x, y = args
if x <= 0 or y <= 0:
raise ValueError('values must be greater than 0')
print('--- validated value', x)
print('--- validated value y', y)
x = x+2
y = y+1
res = f(x, y, **kwargs)
return res
return decor
@validate
def child(x, y):
print('child got value x:', x)
print('child got value y:', y)
return x, y
child(3, 6)
child(0, 0)
示例输出:
--- validated value x 3
--- validated value y 6
child got value x: 5
child got value y: 7
Traceback (most recent call last):
File "/data/projects/test/functions.py", line 212, in <module>
child(0, 0)
File "/data/projects/test/functions.py", line 195, in decor
raise ValueError('values must be greater than 0')
ValueError: values must be greater than 0
我有下面这两个功能。我想先 运行 验证然后 child 但我想用验证装饰 child 所以我可以告诉它 运行 先验证给定的输入然后传递输出到 child 到 运行 就可以了。
def validate(x, y):
print(x, y)
x = x+2
y = y +1
return x, y
def child(x, y):
print(x)
print(y)
return x, y
我该怎么做?
显然,这是行不通的:
def validate(x):
print(x)
x = x+2
return x
@validate
def child(x):
print(x)
return x
我想用装饰器的方式实现这样的目标:
child(validate(2))
编辑:
我有一些方法“data_parsing
”接受输入并对输入的数据进行一些登录。数据可能出现故障,所以我创建了一个 class 方法,首先验证输入的数据。如果数据格式不正确,我会实例化 class 和 运行 验证,首先引发异常。如果成功,我将进入下一个函数调用 data_parsing
() ,该函数调用获取数据并对其进行处理。所以逻辑是:
def execute(data):
validator_object(data).run()
data_parsing(data)
编辑:
def validator(fnc):
def inner(*aaa):
a,b = aaa
a += 4
return fnc(a,b)
return inner
@validator
def child(*aaa):
a,b = aaa
print(a)
return a
a = 1
b = 2
child(a, b)
请注意,@decorator
形式应用于函数声明阶段,它会立即包装目标函数。
您可以针对您的案例使用以下实现:
def validate(f):
@functools.wraps(f)
def decor(*args, **kwargs):
x, y = args
if x <= 0 or y <= 0:
raise ValueError('values must be greater than 0')
print('--- validated value', x)
print('--- validated value y', y)
x = x+2
y = y+1
res = f(x, y, **kwargs)
return res
return decor
@validate
def child(x, y):
print('child got value x:', x)
print('child got value y:', y)
return x, y
child(3, 6)
child(0, 0)
示例输出:
--- validated value x 3
--- validated value y 6
child got value x: 5
child got value y: 7
Traceback (most recent call last):
File "/data/projects/test/functions.py", line 212, in <module>
child(0, 0)
File "/data/projects/test/functions.py", line 195, in decor
raise ValueError('values must be greater than 0')
ValueError: values must be greater than 0