[英]python equivalent of quote in lisp
In python what is the equivalent of the quote operator? 在python中,什么是引用运算符的等价物? I am finding the need to delay evaluation.
我发现有必要推迟评估。 For example, suppose in the following lisp psuedocode I have:
例如,假设我在下面的lisp伪代码中有:
a = '(func, 'g)
g = something
(eval a)
What I am doing is deferring evaluation of g
till a later time. 我正在做的是将
g
评估推迟到以后的时间。 This is necessary because I want to define g
later. 这是必要的,因为我想稍后定义
g
。 What is the equivalent idea of this psuedocode in python? 在python中这个psuedocode的等价思想是什么?
a = lambda: func(g)
g = something
a()
This isn't quite the most literal translation - the most literal translation would use a string and eval
- but it's probably the best fit. 这不是最直接的翻译 - 最直接的翻译将使用字符串和
eval
- 但它可能是最合适的。 Quoting probably isn't what you wanted in Lisp anyway; 无论如何,引用可能不是你想要的Lisp; you probably wanted to
delay
something or create a lambda
. 你可能想
delay
一些事情或创建一个lambda
。 Note that func
and g
are closure variables in the lambda
function, rather than symbols, so if you call a
from an environment with different bindings for func
or g
, it'll still use the variables from a
's environment of definition. 需要注意的是
func
和g
是在封闭变量lambda
功能,而不是符号,所以如果你调用a
从不同的绑定环境func
或g
,它仍然会使用这些变量从a
定义的环境。
Using eval
for delaying evaluation is bad, both in Lisp and Python. 在Lisp和Python中使用
eval
来推迟评估是不好的。
in Python, and in Lisp, you can delay evaluation using a closure: 在Python和Lisp中,您可以使用闭包延迟评估:
def print_it(x):
def f():
print g(x)
return f
f = print_it(42)
def g(x):
return x * x
f()
Please note that what is captured in a closure is not the value of a variable, but the variable itself and this is sometimes surprising: 请注意,在闭包中捕获的不是变量的值 ,而是变量本身,这有时令人惊讶:
fa = []
for x in range(10):
def g():
print x
fa.append(g)
for f in fa:
f() # all of them will print 9
x = 42
fa[0]() # the output will be 42
to solve this problem (that can also be present in Common Lisp) you may see things like: 要解决这个问题(也可以出现在Common Lisp中),您可能会看到以下内容:
for x in range(10):
def g(x = x):
print x
fa.append(g)
or (in CL) things like 或者(在CL中)像
(let ((a a))
(lambda () (print a)))
Python also has a lambda
special form for anonymous functions, but they are limited to one single expression and cannot contain any statement. Python还有一个用于匿名函数的
lambda
特殊形式,但它们仅限于一个表达式,不能包含任何语句。 A locally def
-ined function instead is a regular function without any limitations. 本地
def
-ined功能反而是没有任何限制的常规功能。
for x in range(10):
# print is a statement in Python 2.x and cannot be in a lambda
fa.append(lambda x=x: sys.stdout.write(str(x) + "\n"))
Finally Python 2.x has a syntax limitation and closed-over variables are read-only because if there is an assignment (or augmented-assignment) in a function there are only two possibilities: 最后,Python 2.x具有语法限制,并且封闭变量是只读的,因为如果函数中存在赋值(或扩充赋值),则只有两种可能性:
global x
) global x
声明) and in particular it's ruled out that a variable being assigned could be a local of an enclosing function scope. 特别是它排除了被赋值的变量可以是封闭函数范围的局部变量。
Python 3.x removed this limitation by providing a new possible declaration nonlocal x
and now the famous adder
example can be implemented as Python 3.x通过提供一个新的可能声明
nonlocal x
消除这个限制,现在着名的adder
示例可以实现为
def adder(x):
def f(y):
nonlocal x
x += y
return x
return f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.