Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
exec是Scheme'的Python对应物吗;什么是“应用”?_Python_Scheme - Fatal编程技术网

exec是Scheme'的Python对应物吗;什么是“应用”?

exec是Scheme'的Python对应物吗;什么是“应用”?,python,scheme,Python,Scheme,我找到了Python内置函数eval,但找不到类似于Scheme的apply的函数 我已经详尽地检查了其他Python关键字,但没有一个与apply匹配 候选人可能是exec: In [14]: eval("print('testing')") testing In [15]: exec("print('testing')") testing >>> args = [1, 2, 3] >>> print(*args) 1 2 3 在

我找到了Python内置函数
eval
,但找不到类似于Scheme的
apply
的函数

我已经详尽地检查了其他Python关键字,但没有一个与
apply
匹配

候选人可能是
exec

In [14]: eval("print('testing')")      
testing
In [15]: exec("print('testing')")      
testing
>>> args = [1, 2, 3]
>>> print(*args)
1 2 3
在计划中:

> (eval '(display 'testing))
testing
> (apply display '(testing))
testing

Python的
exec
是否对应于Scheme的
apply

否。Scheme的
apply
用于将函数应用于存储在列表中的参数。Python的方法是
*
,而不是
exec

In [14]: eval("print('testing')")      
testing
In [15]: exec("print('testing')")      
testing
>>> args = [1, 2, 3]
>>> print(*args)
1 2 3

Python有一个
apply
函数匹配方案的
apply
,但是为了使用
*
符号,它被删除了。

要在Python中定义apply:

def apply(f, arg_list):
    return f(*arg_list)
方案示例:

(define (pythagoras a b)
  (sqrt (+ (* a a) (* b b))))

(apply pythagoras '(3 4))  ; Returns: 5
类似的Python示例:

import math

def pythagoras(a, b):
    return math.sqrt(a**2 + b**2)

apply(pythagoras, [3, 4])  # Returns: 5.0
旁注(基于您对此类事物的明显兴趣)

Python的
eval

eval('pythagoras(3, 4)')
鉴于在计划中:

(eval '(pythagoras 3 4))
(eval "Hello")
(eval `(,(if (> x 0) + -) 100 ,(pythagoras 3 4)))
如您所见,Python的
eval
要求您处理字符串、字节或“代码对象”。方案的
eval
接受方案。见:

您可能有兴趣阅读(来自计算机程序的结构和解释,第4章,元语言摘要),了解Scheme的
eval
apply
的含义


注意,在Python中使用<代码> EVA/CODE是非常令人沮丧的(如果你真的需要使用它,请考虑一下)。在这两种语言中,如果可以使用其他方法解决问题,通常建议您避免使用

eval

定义
apply
?实现将过程应用于参数@Chris_RandsIn Python将是
print(*args)
…!?嗯,疯狂的想象,毫无意义@你在找
map()