This question already has answers here:
Scoping in Python 'for' loops
(8 answers)
Closed 9 years ago.
I have tried this code :
for i in range(10)
print(line, i)
print(line, i)
and the program executed without error, so why is i declared even after the for statement, it must no longer exist.
Yes, your iteration variable isn’t deleted when the loop is finished. As the documentation puts it: „Names in the target list are not deleted when the loop is finished”.
This has to do with variable scopes. As has been pointed out, the variable i exists within the scope of the function you’re in. A loop does not create an extra scope in python.
The scope is within a function, not a loop. A little different than other programming languages.
Related
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 3 years ago.
def convert(target):
#Some code here
target = 20
convert(x)
print(x)
I expected the output to be 20, but there is always an error. "NameError: Name x is not defined" Is there some way that I could fix that?
It sounds like you are trying to pass the value by reference, which goes against the grain of Python, which passes variable by assignment. The easiest "solution" is to refactor your code so it returns the new value, as others have suggested.
In some languages (e.g. PHP), you can specify that certain arguments are to be passed by reference (e.g. by prefixing the variable name with a "&"), but in practice this often makes the code harder to read and debug.
For more information, see:
How do I pass a variable by reference?
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 3 years ago.
I wrote something like this where I am able to access a variable that is used through pythons in for loop syntax and then being able to access that variable, I was wondering if there is a specific reason to being able to do this
for i in array:
do_something(i)
b = i
From Short description of the scoping rules?: "The for loop does not have its own namespace"
This means that variables declared in the loop are available for the rest of the code in the same scope as the loop itself
This question already has answers here:
Keep the lifespan of variable after multiple function calls?
(4 answers)
Closed 5 years ago.
Assuming:
def myfunc(x):
listv = []
listv.append(x)
is there a keyword to stop a variable (listv) from being reassigned?
Let's suppose that NRA is a keyword:
def myfunc(x):
NRA listv = []
listv.append(x)
...line will be read and not reassigned but the variable still active appending new values for every function call. I know about the GLOBAL variable, but I just want to know is the keyword exists!
Variables in functions are not supposed to be persistent between function calls. Because functions are meant to be reusable codes that can be called from different context in the program. So to your answer, NO! There's no keyword for making a variable declared in function persistent.
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 5 years ago.
In this code, why do a and b not get destroyed at the ends of their respective blocks?
flag = True
if flag:
a = 1
for i in range(2):
b = 2
print(a, b)
Instead, this code prints 1 2. Why does Python allow this? When can I rely on this behavior?
Read up on the scoping rules for Python. In short, a scope is started with a new module: function, method, class, etc. Mere control flow statements (e.g. if and for) do not start a new scope. The scope of a variable is from its firs definition to the end of that scope.
Since this example has only one scope, each variable is good from its first assignment to the end of the file.
Is that enough to clear up the problem?
This question already has answers here:
Can you explain closures (as they relate to Python)?
(13 answers)
Closed 6 years ago.
Let's suppose to have this simple code:
def my_outer_function():
outer_var = 123
def my_inner_function():
return outer_var + 1
return my_inner_function
get_inner = my_outer_function()
get_inner()
I wonder there isn't any kind of runtime error. outer_var - the variable of the outer function - is available only when that function is running on, i.e. it vanishes when my_outer_function ends. But when I call get_inner(), my_outer_function() is already ended so I would have bet on a runtime error since my_inner_function couldn't find outer_var.
How do you explain all this?
the variable of the outer function is available only when that function is running on, i.e. it vanishes when my_outer_function ends.
That's not entirely true. The variable is available in my_outer_function's scope. my_inner_function has the scope of its own declarations and its parent's scope.
my_inner_function references a variable outside of its own scope, and so those references are bound to my_inner_function as a closure when its parent's scope is no longer available. To learn more about closures, see Can you explain closures (as they relate to Python)? (taken from Paul Rooney's comment)