I defined a function inside of a function in a program (Python):
def func1:
x = 5
def func2(y):
return x*y
x = 4
print func2(5)
How does the scope for x here work, with AND without the line that redefines x=4? Is this language-specific behavior?
First of all: you're being too lazy. Before you write code here, at least feed it to an interpreter or compiler for the language in question to weed out the obvious problems. In this case, there are two:
()
on the definition offunc1
.func2
being undefined where it is called.So let's try
Three lines are commented; they produce errors when uncommented.
As you can see, Python definitions, of both variables and functions, are local by default: when defined within a function, they only apply within that function, and cannot be used elsewhere.
Variables defined outside a function can be used inside the function. However, they can only be read, not written. This explains why we can use
x
insidefunc2
but cannot assign to it; whenx = x + 1
is uncommented, it is interpreted as applying to a differentx
variable insidefunc2
, which is why we'll get an error complaining that it is used uninitialized.In addition, Python has global variables, which can be used anywhere in the program; such variables must be marked
global
explicitly.This is very similar to PHP (in which variables can also be marked as
global
) and fairly similar to many other languages, such as C (which has noglobal
, but the similarextern
).You're trying to have a function (
func2
) use a variable (x
) defined in the context of its definition, then call the function outside the context wherex
is defined and still have it work. Functions that can do this are known as closures.We've just seen that Python only has partial support for closures: the function can read such variables, but cannot modify them. This is already better than many other languages. However, a full closure can also modify such variables.
For instance, the following is valid Perl code:
Full closure support is traditionally associated with dynamic scoping (i.e. making definitions executable statements, evaluated at run time). This is because it was introduced by Lisp, which was dynamically scoped, and support for it is rare in languages that use static (lexical) scoping (in which the scope of definitions is determined at compile time, not at run time). But all of the variables and function definitions in this Perl code are statically scoped.