我有可以接受 List
或 str
的函数。返回类型应与输入类型匹配。例如,如果给定一个 str
,该函数应该返回一个 str
。
这是一个说明这一点的玩具示例:
def swap_first_and_last(a: Union[List, str]) -> Union[List, str]:
# Not in-place.
STR = isinstance(a, str)
a = list(a)
a[0], a[-1] = a[-1], a[0]
return "".join(a) if STR else a
这是一个实际的例子:
def next_permutation(a: Union[List, str]) -> Union[List, str]:
"""
Not in-place.
Returns `None` if there is no next permutation
(i.e. if `a` is the last permutation).
The type of the output is the same as the type of the input
(i.e. str input -> str output).
"""
STR = isinstance(a, str)
a = list(a)
N = len(a)
i = next((i for i in reversed(range(N-1)) if a[i] < a[i + 1]), None)
if i is None:
return None
j = next(j for j in reversed(range(i+1, N)) if a[j] >= a[i])
a[i], a[j] = a[j], a[i]
a[i + 1:] = reversed(a[i + 1:])
return "".join(a) if STR else a
如您所见,只有几行专门用于处理 str
输入与 List
输入,即:
# preprocess
STR = isinstance(a, str)
a = list(a)
# main logic
...
# postprocess
return "".join(a) if STR else a
我可以使用装饰器来进行这种轻微的预处理和后处理吗?
最佳答案
是的,你可以使用这样的装饰器:
from typing import Union, List
def pre_and_post_processing(func):
def inner(a: Union[List, str]) -> Union[List, str]:
STR = isinstance(a, str)
a = list(a)
b = func(a)
return "".join(b) if STR else b
return inner
@pre_and_post_processing
def swap_first_and_last(a: List) -> List:
a[0], a[-1] = a[-1], a[0]
return a
print(swap_first_and_last("asd")) # -> dsa
print(swap_first_and_last(["asd", "ds", "sds"])) # -> ['sds', 'ds', 'asd']
请注意,swap_first_and_last
函数现在获取并返回一个List
。
关于python - 我可以使用 python 装饰器根据输入类型预处理输入和后处理输出吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67378555/