gpt4 book ai didi

python - 我可以使用 python 装饰器根据输入类型预处理输入和后处理输出吗?

转载 作者:行者123 更新时间:2023-12-04 14:58:40 25 4
gpt4 key购买 nike

我有一些函数可以使用 Liststr。返回类型应与输入类型匹配。例如,如果给定一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com