gpt4 book ai didi

python - Lisp 的 apply 和 funcall 与 Python 的 apply

转载 作者:太空宇宙 更新时间:2023-11-03 18:49:06 26 4
gpt4 key购买 nike

Lisp 的 APPLY 用于调用存储在列表中的计算参数的函数。(根据 Rainer 的评论修改)

例如,以下代码将 (list 1 2 3) 更改为 (+ 1 2 3)。

(apply #'+ '(1 2 3)) 

但是,Python 的 apply 做的是 Lisp 的 funcall 做的,除了一些细微的差别(输入以元组/列表的形式给出)

(defun add (x y) (+ x y))(funcall #'add 1 2) or(funcall #'(lambda (x y) (+ x y)) 10 2)
apply(lambda x,y : x+y, [1,2])

你怎么看? Lisp 的 funcall 和 Python 的 apply 之间还有更多区别吗?

最佳答案

Is there any reason why Python chose the name apply not funcall?

因为它是 Python,而不是 LISP。不需要具有相同的名称,funcall 是一个 LISP 命令,而 apply 在 Python 中是不同的东西。

apply 在 Python 中已弃用,请使用扩展的调用语法。

旧语法:

apply(foo, args, kwargs)

新语法:

foo(*args, **kwargs)

关于python - Lisp 的 apply 和 funcall 与 Python 的 apply,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3856917/

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