Python 和Scheme 之间的变量作用域有什么区别?

发布于 2024-08-11 18:47:20 字数 97 浏览 2 评论 0原文

请参阅变量范围。

我试图找出这两者之间的区别。

例如,方案函数中的匿名函数可以访问该函数的本地变量。 python有这个吗?

谢谢!

Refering to Variable Scoping.

I'm trying to figure out what are the differences between those 2.

For example, Anonymous functions in a scheme function has access to the variables local to that function. Does python have this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十二 2024-08-18 18:47:20

在 Python 中,变量作用域可以是全局变量,也可以是函数变量。在Scheme中,范围可以是任何块。

例如,在Scheme中,您可以在循环内部定义一个变量,并且无法从循环外部访问它。在Python中,作用域是整个函数,这个变量会从循环中“泄漏”到函数的其余部分。

关于您的具体问题:请注意,Python 中的匿名(lambda)函数非常有限(只有一个表达式,没有 if/then 语句、循环等),因此您通常定义完整的(命名)内部函数。在这种情况下,作用域规则类似于Scheme:内部函数可以访问外部函数的变量(创建闭包),并且外部函数不能访问内部函数内部定义的变量。

简而言之:词法作用域和闭包按预期工作;只需记住范围粒度是每个函数,而不是每个块。

In Python variable scope can be either global or function. In Scheme, the scope can be any block.

For example, in Scheme you could define a variable inside a loop, and it wouldn't be accessible from outside the loop. In Python, the scope being the whole function, this variable would 'leak' out of the loop into the rest of the function.

About your specific question: note that anonymous (lambda) functions in Python are horribly limited (just a single expression, no if/then statements, loops, etc.), so you usually define complete (named) inner functions. In this case the scope rules are similar to Scheme: the inner function can access the outer function's variables (creating a closure), and the outer function can't access variables defined inside the inner function.

In short: lexical scoping and closures work as expected; just remember that the scoping granularity is per function, not per block.

羅雙樹 2024-08-18 18:47:20

微妙的问题。是的,Python lambda 关闭封闭函数中的变量。两种语言都有词法范围。但细节却截然不同。

在Scheme中,通常使用(let)(letrec)(lambda)引入一个变量,并且它的范围非常明确变量是.此外,只要变量在作用域内,您就可以使用 (set!) 为其赋值。

在Python中,隐式分配一个名称会产生一个局部变量,其作用域是整个函数。因此,即使您可以通过名称​​访问外部变量,如果您想分配到外部作用域中的变量,则必须使用全局声明(或在 Python 3.0 nonlocal 中)。

Python 也有类。类成员并不是真正的词法变量;而是真正的词法变量。方法不会关闭它们。

>>> def top(x):
...   class C:
...     x = 1  # foo does not close on this 'x'.
...     def foo(self):
...       return x  # this refers to the argument of top
...   return C().foo()
... 
>>> top(2)
2

有关更多信息,请参阅 Python 语言参考: http://docs.python.org/reference/ executionmodel.html#命名

Subtle question. Yes, Python lambdas close on variables in the enclosing function. Both languages have lexical scoping. But the details are quite different.

In Scheme, you usually introduce a variable with (let) or (letrec) or (lambda) and it's pretty explicit what the scope of the variable is. Also, whenever a variable is in scope, you can assign to it using (set!).

In Python, assigning to a name implicitly makes a local variable whose scope is the entire function. So even though you can access outer variables by name, if you want to assign to a variable in an outer scope, you must use a global declaration (or in Python 3.0 nonlocal).

Python also has classes. Class members aren't really lexical variables; methods do not close over them.

>>> def top(x):
...   class C:
...     x = 1  # foo does not close on this 'x'.
...     def foo(self):
...       return x  # this refers to the argument of top
...   return C().foo()
... 
>>> top(2)
2

See the Python language reference for more: http://docs.python.org/reference/executionmodel.html#naming

一个人的旅程 2024-08-18 18:47:20

是的,Python 能够访问该函数内匿名函数(在 Python 中,这称为 lambda 函数)的本地变量。

我不完全确定这是否回答了您的问题,所以如果没有,请发布更多详细信息

Yes, Python has the ability to access variables that are local to an anonymous function (in Python, this is called a lambda function) within that function.

I'm not entirely sure if that answered you question, so if it didn't, please post some more detail

停顿的约定 2024-08-18 18:47:20

正如您所问的那样,Scheme 和 Python 都是词法范围的。

def make_adder(n):
    when = datetime.now()
    def adder(k):
        print "The adder you created at %s has been called!" % when
        return k + n
    return adder

Scheme and Python are both lexically scoped, in the way that you seem to be asking.

def make_adder(n):
    when = datetime.now()
    def adder(k):
        print "The adder you created at %s has been called!" % when
        return k + n
    return adder
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文