Python overwriting variables in nested functions

Question:

Suppose I have the following python code:

def outer():
    string = ""
    def inner():
        string = "String was changed by a nested function!"
    inner()
    return string

I want a call to outer() to return “String was changed by a nested function!”, but I get “”. I conclude that Python thinks that the line string = "string was changed by a nested function!" is a declaration of a new variable local to inner(). My question is: how do I tell Python that it should use the outer() string? I can’t use the global keyword, because the string isn’t global, it just lives in an outer scope. Ideas?

Asked By: Ord

||

Answers:

In Python 3.x, you can use the nonlocal keyword:

def outer():
    string = ""
    def inner():
        nonlocal string
        string = "String was changed by a nested function!"
    inner()
    return string

In Python 2.x, you could use a list with a single element and overwrite that single element:

def outer():
    string = [""]
    def inner():
        string[0] = "String was changed by a nested function!"
    inner()
    return string[0]
Answered By: Sven Marnach

To add to Sven’s answer:

In Python 2.x, you can only read outer scope variables from the inner scope. Assigning will just create a new local (i.e. inner scope) variable that hides the outer scope one.

If you want to read and modify, you can use a dict to hold your variables in the outer scope, and then access them via the dict in the inner scope, also keeping your code fairly clean and readable in the presence of multiple outer scope vars:

def outer():
    # hold some text, plus the number of spaces in the text
    vars = {'text': 'Some text.', 'num_spaces': 1}
    def inner():
        # add some more text
        more_text = ' Then some more text.'
        vars['text'] += more_text
        # keep track of the number of spaces
        vars['num_spaces'] += more_text.count(' ')
    inner()
    return vars['text'], vars['num_spaces']

output:

>>> outer()
('Some text. Then some more text.', 5)
Answered By: nitsas

You can also get around this by using function attributes:

def outer():
    def inner():
        inner.string = "String was changed by a nested function!"
    inner.string = ""
    inner()
    return inner.string

Clarification: this works in both python 2.x and 3.x.

Answered By: crunk1

This happens to me way too often, when I was writing a function and I suddenly realize that it could be a good idea to have a smaller helper function, but not really useful anywhere else. which naturally makes me want to define it inside as a nested function.

but I had experience with JAVA anonymous object(ie: define a runnable), and the rule was that the anonymous object makes a hard copy of its outer environment, in this case variables of the outer scope. Thus if the outer variable is a immutable (int,char), they can not be modified by anonymous object as they are copied by value whereas if its a mutable (collection, objects), they can be changed…since they are copied by “pointer” (their address in memory)

if you know about programming, think of it as pass by value and pass by reference.

in python, it’s very much the same. x=123 is an assignment, they give the variable x a new meaning (not modify the old x), list[i]/dict[key] are object access operations, they really modify things

to conclude, you need a mutable object…in order to modify (even though you can access a tuple using [], you can not use it here since its not mutable)

Answered By: watashiSHUN
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.