我们有多个类似部分的函数,具有带有 args 和 kwargs 的相同类型注释,例如:
def fruit(fruit_name: str, fruit_class: Type, arg1: int, arg2: float, arg3: tuple):
pass
def apple(*args, **kwargs):
return fruit("apple", *args, **kwargs)
def orange(*args, **kwargs):
return fruit("orange", *args, **kwargs)
def banana(*args, **kwargs):
return fruit("banana", *args, **kwargs)
我可以以某种方式为函数
apple
、orange
、banana
创建一个模板并将其分配给它们吗?
我想到了 Protocol
和 __call__
定义,但不清楚如何将其分配给函数
您可以使用
functools.partial
,而不是函数:
from typing import Callable, Type
from functools import partial
Fruit = ... # whatever fruit returns
# type of fruit(), but without the initial string parameter
FruitMaker = Callable[[Type, int, float, tuple], Fruit]
def fruit(fruit_name: str, fruit_class: Type, arg1: int, arg2: float, arg3: tuple) -> Fruit:
...
apple: FruitMaker = partial(fruit, "apple")
orange: FruitMaker = partial(fruit, "orange")
banana: FruitMaker = partial(fruit, "banana")
另一种可能性是重构
fruit
以获取名称,并 return 关闭水果名称的函数。
def fruit_maker(fruit_name: str) -> FruitMaker:
def fruit(fruit_class: Type, arg1: int, arg2: float, arg3: tuple):
...
return fruit
apple = fruit_maker("apple")
orange = fruit_maker("orange")
banana = fruit_maker("banana")
“Builder”模式可能适合您:
class Fruit: pass
class FruitBuilder:
def __init__(self, fruit_name: str):
pass
def build(self, fruit_class: type, arg1: int, arg2: float, arg3: tuple) -> Fruit:
pass
class AppleBuilder(FruitBuilder):
def __init__(self):
super().__init__("apple")
class OrangeBuilder(FruitBuilder):
def __init__(self):
super().__init__("orange")
AppleBuilder().build(...)