This question already has answers here:
Variables declared outside function
(3 answers)
Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?
(9 answers)
Closed 6 days ago.
Why the following code doesn't work as it is but would work after commenting either print(x) or x=1?
def f():
x = 1
def g():
print(x)
x = 1
g()
f()
Related
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 2 years ago.
x = 10
def double(y):
return 2 * x
print(double(x))
Output is 20
As far as I know, it should return None because in function "double" I double x which is undefined that block.
x is defined outside of the function scope as a global variable that is also available in the function
It is because x is a global variable, so when you call double it is just multiplying x by 2 no matter what
if you were to say put
x = 10
def double(y):
return 2 * x
print(double(40))
you would still get 20 as you are returning the variable x multiplied by 2.
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 2 years ago.
I want function(x) to change the value of whatever global variable is put into it, say add one to the variable, like so:
>>> a = 1
>>> function(a)
>>> print(a)
2
I tried:
def function(x):
global x
x = x + 1
However, this returns a SyntaxError name 'x' is parameter and global...
a=1 #global variable
def function():
global a
a=a+1 #operation on variable a
return a
function()
is this answer your query?
This question already has answers here:
Can we have assignment in a condition?
(10 answers)
How to avoid writing request.GET.get() twice in order to print it?
(10 answers)
How to do variable assignment inside a while(expression) loop in Python?
(6 answers)
Closed 4 years ago.
Is it possible to modify variables in if statements, loops, and function calls in Python like you can in C and Java?
Ex:
i=0
while((i+=1)<10): #invalid syntax
print(i)
If not why is that?
as long as the datatype is mutable
def modified(data):
data['a'] = 5
x = {'b':7}
modified(x)
print(x)
def increment_a(data):
data['a'] += 1
return data['a']
x = {'a':1}
while increment_a(x) < 10:
print(x)
however strings and integers are immutable
This question already has answers here:
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
Changing one list unexpectedly changes another, too [duplicate]
(5 answers)
Closed 4 years ago.
When I run the following block of code in python, it seems like for input x as a integer, func3 and func4 are correctly capturing the scope of the input. However, if x is an array, func2 sees input x not as [10], but as [9]. Why? Can functions in python change variables outside the scope of that specific function? Ie, it seems like func1 is modifying the global x.
def func1(x):
x[0] -= 1
return(x)
def func2(x):
x[0] -= 2
return(x)
def func3(x):
x -= 1
return(x)
def func4(x):
x -= 2
return(x)
if __name__ == "__main__":
x = [10]
print(func1(x)) # [9]
print(func2(x)) # [7]
x = 10
print(func3(x)) # 9
print(func4(x)) # 8
This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 4 years ago.
x = 5
def foobar():
print (x) #Prints global value of x, which is 5
x = 1 #assigns local variable x to 1
foobar()
Instead, it throws a
UnboundLocalError: local variable 'x' referenced before assignment
What am I misunderstanding in the comments? Note, I understand if i do x=x+1, it'll throw error due to 'accessing value of local scope x beforei it's defined', but in this case I'm doing x=1, which does not require reading of existing value of x! This is NOT a duplicate question.
If you want to change a global variables value in a function in python, you have to do
x = 5
def foobar():
global x
print (x) #Prints global value of x, which is 5
x = 1 #assigns local variable x to 1
foobar()