简体   繁体   English

python相当于lisp中的引用

[英]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. 需要注意的是funcg是在封闭变量lambda功能,而不是符号,所以如果你调用a从不同的绑定环境funcg ,它仍然会使用这些变量从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具有语法限制,并且封闭变量是只读的,因为如果函数中存在赋值(或扩充赋值),则只有两种可能性:

  1. The variable is a global (and has been previously declared so with global x ) 变量是全局变量(之前已使用global x声明)
  2. The variable is a local 变量是本地的

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM