-
Book Overview & Buying
-
Table Of Contents
Building Programming Language Interpreters
By :
In some languages, the name of a variable in a function is always scoped to the entire function. Let’s look at the following Python code:
def do_something():
for a in 1,2,3:
print(a)
print(a)
If Python is your primary language, the output here would be obvious, as the scope of the name is always defined by the function itself. The a variable, therefore, will have the value that it had in the last iteration of the loop. The output is as follows:
1
2
3
3
If you come from another language, such as Perl, you would have expected that code to be invalid, because the variable was defined inside the loop, and therefore it shouldn’t be reachable after the loop is complete.
This, however, provides some simplification to the interpreter, because it only needs to initialize a single lexical pad to the entire function, and every variable defined within the function will be referenced directly from it.
There is also...
Change the font size
Change margin width
Change background colour