+ 2
Need explain work def
I have no idea how to explain why it isn’t printed 42 I will appreciate any hint x =[1,2,3] def f(x): a = 42 x[1] = 42 x=a f(x) print(x) https://code.sololearn.com/cW7eI4ysovI8/?ref=app
7 Answers
+ 1
#Variable defined in a function are local variables belongs to the function only it won't available outside of that function.
x=1
def f():
y=2 #local variable to function f() only
print(y,x)
f()
print(x) #global variable x, you can use it function as well
print(y) #error undefined variable
#if a variable not found , then it search in global variables.
x=12
def f():
print(x)
f()
"""
x=12
def f(x):
print(x) #global x
x=32 #it shadows global x from now
print(x) #local x exist in function only
print(x) #global x
f(x)
print(x) #global x
"""
#hope it clears...
+ 3
When you are printing 'x' ,it is a global variable which is a list and modified in function as well but x=a in function cause to create local variable again in function and shadows global variable there after in function.Ksndr
To see defference ,check this code
x =[1,2,3]
def f(x):
a = 42
x[1] = 42
x=a
print(x)
#x[1]=40 #error now
#return x #if you want to return 42
f(x)
print(x)
#print(f(x)) #if you returning any value
+ 3
List x is a global variable and x=a cause to create another variable x local to function. And there onwards its local variable x you are referring.
And in printing its a previous global variable not one you defined in function.
Run the above code again I posted to see the difference.. uncomment x[1]=40 produce error which is not error before x=a
It does not returning any.
If you want x= 42 to be printed ,then return that value to back to assign x as x= f(x) by uncomment return x
+ 2
I see,Jayakrishna🇮🇳
But why return [1,42,3] insteed 42, it was last chage x in the def?
+ 2
that's what i got
I have a question because I underestimated two moments (most likely it was my carelessness)
1) the def, strictly speaking, did not return anything (if there were a return, I would get 42)
2) being at the mercy of first mistake, I did not betray the meaning that I gave the def access to change my global variable, without return
it all fits
but
there was a new question:
what rules will help to avoid such a mistake in coding? is it enough to just put a return (control the result of the created functions)?
Thank you for your patience Jayakrishna🇮🇳 !
What do you thinkIlya Khimchenko ?
+ 1
Sorry, Jayakrishna🇮🇳 I still miss something important
I think ive done what you wrote:
x =[1,2,3]
print(x,'global before f')
def f(d):
a = 42
d[1] = 42
print('\t',d,'local change #1')
d = a
print('\t',d,'local change #2, last!')
f(x)
print(x, 'global after f')
#[1, 2, 3] global before f
# [1, 42, 3] local change #1
# 42 local change #2, last!
#[1, 42, 3] global after f
And my question still alive
Why not 42
+ 1
I don't understanding your question fully.. is you want affect global variable inside function?
return value #statement just returns a value to calling statement back, not any other(changes in function)..
Whenever you use a assign statement then its creates a new variable in that scope. So if you use a assignment in function then its creates a new variable local to function.
x=1 #global x
def fun():
print(x) # global x
x=2
print(x) #creates a new local variable, shadows global x, and available within the function only..
fun()
if you want use global variable thought function and want to change value by assigning then you can tell that like:
x=1
def f():
global x #tells to use global x through out f()
x=2 #affects global x, don't creates new
print(x) #1
f()
print(x) #2