classification
Title: Invalid child function scope
Type: behavior Stage: resolved
Components: Versions: Python 3.6, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: gasokiw, r.david.murray, ronaldoussoren
Priority: normal Keywords:

Created on 2018-05-15 20:56 by gasokiw, last changed 2018-06-04 16:23 by r.david.murray. This issue is now closed.

Files
File name Uploaded Description Edit
example and workaround.zip gasokiw, 2018-05-15 20:56 Bug example and workaround for it
Messages (3)
msg316725 - (view) Author: gasokiw (gasokiw) Date: 2018-05-15 20:56
When you try to reassign variable with same name as one of parameters/arguments of parent function to local scope in child function, even if it doesn't actually get ran, the argument/parameter is not passed to child function anymore. 
In practice this happens when you make decorator with arguments/parameters and make wrapper function inside it.

As workaround you can redeclare those arguments/parameters in new variables in parent function then rewrite them back in child function. ( as seen in child_function_scope_bodge.py )

Please view attachment to better understand the issue.
msg316730 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-05-15 21:22
Please post an example, and not a zip file.  Given your description (which indeed is not enough to understand what you think the problem is by itself), I think you should be able to post a few lines of python code into the issue in order to explain what you see as the problem.
msg318649 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2018-06-04 12:54
From the zip file, the "bug" is:

# ----

def parent_function2( argument1 ):
        def child_function():
                print( argument1 )
                if False:
                        argument1 = None  # Same function but with fake assignment

        return child_function

# ----

This doesn't work because "argument1" is a local variable in the nested function, and not the same as "argument1" in the outer function.  This is expected behavior.

The workaround from the same zipfile is to assign the argument of the outer function to a local variable in that outer function, that way the value can be used in the inner function. That is a valid way to deal with this, although I'd use a different name in the inner function.

The "nonlocal" keyword in Python 3 might be useful here, but that depends on whether or not you want changes to "argument1" in the inner function to affect the value of "argument1" in the outer function.

All in all I don't think there's a bug in Python here.
History
Date User Action Args
2018-06-04 16:23:30r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: test needed -> resolved
2018-06-04 12:54:33ronaldoussorensetnosy: + ronaldoussoren
messages: + msg318649
2018-05-18 18:07:03terry.reedysetstage: test needed
versions: - Python 3.4, Python 3.5
2018-05-15 21:22:39r.david.murraysetnosy: + r.david.murray
messages: + msg316730
2018-05-15 20:56:10gasokiwcreate