Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, how can I change value of a variable in the parent scope?

for example: assginment statement will declare a new local variable.

foo = 'global'
def func1():
    foo = 'func1'
    def func2():
        foo = 'local variable in func2'

use global declaration will use the foo in global:

def func2():
    global foo
    foo = 'global changed in func2'  #changed the foo value in global scope

how can I change the variable foo in func1 scope?
Thanks for any help.

Edit:

Thank you Brandon Craig Rhodes, I finally understand your meaning.

if there are more than 3 scopes nested, I can store the variable in a list.

foo = ['global', 'function1', 'function2']
def func1():
    foo[1] = 'func1'
    def func2():
        foo[2] = 'func2'
        foo[1] = 'func1 modified in func2'

I just use a global variable actually.


so, if there are two functions nested, we can use

nonlocal foo 

and

global foo 

if there are more than three functions nested,
and each function use variables in other functions scope,
why don't we declare a global list variable?
Thank you for all your help!!!

like image 949
netroyal Avatar asked Mar 21 '11 02:03

netroyal


People also ask

Can you change the value of a variable in Python?

Mutable and immutable types Some values in python can be modified, and some cannot. This does not ever mean that we can't change the value of a variable – but if a variable contains a value of an immutable type, we can only assign it a new value. We cannot alter the existing value in any way.

How do you change the scope of a variable in Python?

Python global keyword is used to change the scope of variables. By default variables inside the function has local scope. Means you can't use it outside the function. Simple use global keyword to read and write a global variable inside a function.

Which keyword allows you to modify a variable which is outside of the current scope?

In Python, global keyword allows you to modify the variable outside of the current scope. It is used to create a global variable and make changes to the variable in a local context.

How do I change the value of a variable permanently in Python?

variable1 = "fi" #start the variable, they can come from the main program instead variable2 = 2 datatowrite = str(variable1) + "\n" + str(variable2) #converts all the variables to string and packs them together broken apart by a new line f = file("/file. txt",'w') f.


2 Answers

In python 3.0 and above you can use the nonlocal keyword.

like image 110
Elson Rodriguez Avatar answered Sep 30 '22 02:09

Elson Rodriguez


In Python 3, I believe, you can use the nonlocal keyword to get permission to modify a variable in an enclosing non-global scope. In Python 2, you cannot reassign foo in an enclosing scope; instead, set foo equal to a mutable object like a list [] and then stick the value you want stored in the list:

def func1():
    foo = [None]
    def func2():
        foo[0] = 'Test'
like image 27
Brandon Rhodes Avatar answered Oct 02 '22 02:10

Brandon Rhodes