Assigning a function with another function as parameter to a variable in Python

huangapple go评论61阅读模式
英文:

Assigning a function with another function as parameter to a variable in Python

问题

以下是代码部分的翻译:

def add(*numbers, squared=False):
   return (sum(numbers) ** 2) if squared else sum(numbers)

def multiply(*numbers, half=False):
    mult = 1
    for i in numbers:
        mult *= i
    return (mult / 2) if half else mult

关于你的问题,你可以使用functools库中的partial函数来实现你想要的功能,以下是代码示例:

from functools import partial

# 创建一个部分应用的函数,其中第一个参数是add函数的返回值,后面的参数是multiply函数的参数
multiply_add = partial(multiply, add(NUMBERS))

# 调用multiply_add函数并传递参数
result = multiply_add((5, 3, 2), half=True)

这样,你可以像上面的示例一样使用multiply_add函数来执行你想要的操作。希望这对你有所帮助。

英文:

Let's say I have the following 2 functions:

def add(*numbers, squared=False):
   return (sum(numbers) ** 2) if squared else sum(numbers)
def multiply(*numbers, half=False):
    mult = 1
    for i in numbers:
        mult *= i
    return (mult / 2) if half else mult

What I want to do is assign a variable (multiply_add) so that I can use it as a function and pass parameters to it but in reality, it's just the multiply function that takes the return of the add function as the first parameter but I'm the one who actually gives the numbers for the add function as well.

So basically something like this:

multiply_add = multiply(add(NUMBERS))

# And I can call it like so:
multiply_add((5, 3, 2), 7, half=True)

# In this case the result would be 35.0 (add = 10, then 10*7 = 70 / 2 = 35)

The above is just an example (and not even a very good one) but I think the idea is clear. Please don't suggest anything that's only applicable to the given code but not the idea itself.

I tried using the partial function from the functools library but I couldn't figure out how to use it so I can achieve what I want.

I'm aware that I can just write a new function and deal with all that but I'm asking if there's a simpler method like using the partial function or something else I can't think of at the moment.

I'm very sorry for this question, I'm sure there's already an answer to it somewhere but I didn't know what to search for and so couldn't find an answer.

答案1

得分: 1

看起来你正在寻找的是lambda。使用它,你可以执行以下操作:

multiply_add = lambda a, b, half: multiply(add(*a), b, half=half)
multiply_add((5, 3, 2), 7, half=True)  # 将返回35

对lambda进行类型提示可以通过从typing模块导入Callable并像这样使用它来实现:

multiply_add: Callable[[tuple[int], int, bool], int | float] = lambda a, b, half: multiply(add(*a), b, half=half)
英文:

it seems what you are looking for is lambda.
Using it you can do

multiply_add = lambda a, b, half: multiply(add(*a), b, half=half)
multiply_add((5, 3, 2), 7, half=True)  # will return 35

type hinting lambda can be done by importing Callable from the typing module and using it like this:

multiply_add: Callable[[tuple[int], int, bool], int | float] = lambda a, b, half: multiply(add(*a), b, half=half)

huangapple
  • 本文由 发表于 2023年2月9日 01:14:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定