Python 和Scheme 之间的变量作用域有什么区别?
请参阅变量范围。
我试图找出这两者之间的区别。
例如,方案函数中的匿名函数可以访问该函数的本地变量。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 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.
微妙的问题。是的,Python lambda 关闭封闭函数中的变量。两种语言都有词法范围。但细节却截然不同。
在Scheme中,通常使用
(let)
或(letrec)
或(lambda)
引入一个变量,并且它的范围非常明确变量是.此外,只要变量在作用域内,您就可以使用(set!)
为其赋值。在Python中,隐式分配一个名称会产生一个局部变量,其作用域是整个函数。因此,即使您可以通过名称访问外部变量,如果您想分配到外部作用域中的变量,则必须使用
全局
声明(或在 Python 3.0nonlocal
中)。Python 也有类。类成员并不是真正的词法变量;而是真正的词法变量。方法不会关闭它们。
有关更多信息,请参阅 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.0nonlocal
).Python also has classes. Class members aren't really lexical variables; methods do not close over them.
See the Python language reference for more: http://docs.python.org/reference/executionmodel.html#naming
是的,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
正如您所问的那样,Scheme 和 Python 都是词法范围的。
Scheme and Python are both lexically scoped, in the way that you seem to be asking.