Python overwriting variables in nested functions

前端 未结 4 2034
天涯浪人
天涯浪人 2020-11-28 09:33

Suppose I have the following python code:

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


        
相关标签:
4条回答
  • 2020-11-28 10:07

    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)

    0 讨论(0)
  • 2020-11-28 10:11

    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)
    
    0 讨论(0)
  • 2020-11-28 10:15

    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]
    
    0 讨论(0)
  • 2020-11-28 10:19

    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.

    0 讨论(0)
提交回复
热议问题