我一直在同时学习Ruby和Python,并且我注意到的一件事是这两种语言似乎以不同的方式处理作用域。下面是我的意思的例子:Ruby和Python中的范围
# Python
a = 5
def myfunc():
print a
myfunc() # => Successfully prints 5
# Ruby
a = 5
def myfunc
puts a
end
myfunC# => Throws a "NameError: undefined local variable or method `a' for main:Object"
看来,DEF块可以访问在Python它的直接范围之外,但不是在Ruby中声明的变量。有人可以确认我的理解是否正确?如果是这样,这些思维范围的方式在编程中是否更常见?
对于Python方面,你是对的(显然更多的是范围界定,但你的措辞并不矛盾任何它)。 – delnan 2013-02-23 16:24:58
可能的Python答案在这里:http://stackoverflow.com/questions/370357/python-variable-scope-question – 2013-02-23 16:27:36