How to change the scope of a variable in a function? Python
This question already has an answer here:
Think of them as being part of the function. When the function ends, all its variables die too.
x=2
y=3
def func(x,y):
x=200
y=300
func(x,y) #inside this function, x=200 and y=300
#but by this line the function is over and those new values are discarded
print(x,y) #so this is looking at the outer scope again
If you want a function to modify a value in exactly the way you have written it, you could use a global
but this is VERY bad practice.
def func(x,y):
global x #these tell the function to look at the outer scope
global y #and use those references to x and y, not the inner scope
x=200
y=300
func(x,y)
print(x,y) #prints 200 300
The problem with this is that it makes debugging a nightmare in the best case, and utterly incomprehensibly impossible in the worst case. Things like these are commonly known as "side effects" in functions -- setting a value you don't need set and doing so without explicitly returning it is kind of a Bad Thing. Generally the only functions you should write that modify items in-place are object methods (things like [].append()
modify the list because it's silly to return a new list instead!)
The RIGHT way to do something like this would be to use a return value. Try something like
def func(x,y):
x = x+200 #this can be written x += 200
y = y+300 #as above: y += 300
return (x,y) #returns a tuple (x,y)
x = 2
y = 3
func(x,y) # returns (202, 303)
print(x,y) #prints 2 3
Why didn't that work? Well because you never told the program to DO anything with that tuple (202, 303)
, just to calculate it. Let's assign it now
#func as defined above
x=2 ; y=3
x,y = func(x,y) #this unpacks the tuple (202,303) into two values and x and y
print(x,y) #prints 202 303
链接地址: http://www.djcxy.com/p/20838.html
上一篇: 如何在python中通过引用分配变量?
下一篇: 如何更改函数中变量的范围? 蟒蛇