Issue33527
Created on 2018-05-15 20:56 by gasokiw, last changed 2022-04-11 14:59 by admin. 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) * ![]() |
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) * ![]() |
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 |
| 2022-04-11 14:59:00 | admin | set | github: 77708 |
| 2018-06-04 16:23:30 | r.david.murray | set | status: open -> closed resolution: not a bug stage: test needed -> resolved |
| 2018-06-04 12:54:33 | ronaldoussoren | set | nosy:
+ ronaldoussoren messages: + msg318649 |
| 2018-05-18 18:07:03 | terry.reedy | set | stage: test needed versions: - Python 3.4, Python 3.5 |
| 2018-05-15 21:22:39 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg316730 |
| 2018-05-15 20:56:10 | gasokiw | create | |
