![]() |
unbounded variable - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: unbounded variable (/thread-41562.html) |
unbounded variable - akbarza - Feb-06-2024 hi in code: ''' ref:https://www.bhavaniravi.com/python/advanced-python/unbound-variables-in-python ''' def take_sum(a, b, c): return a+b+c def main1(): print(take_sum) # output is as :<function take_sum at 0x1031c84c0> if "take_sum" not in globals(): print("take_sum is not in globals()") else: print("take_sum is in globals()") print(globals()) print('-'*30) def main(): if "take_sum" not in globals(): take_sum = lambda x,y,z: x+y+z print(take_sum) main1() main()output has the below error: '''I read the explanation given in the address in the docstring, but I did not understand. (I selected the thread subject from there). what is the problem?plz, explain thanks RE: unbounded variable - deanhystad - Feb-06-2024 Variables are "created" when Python code is parsed, not when it is executed. Before main() was ever called, the code for main "defined" a local vairable named "take_sum". You can see that if you look at the generated bytecode. Below I simplify the function to focus on the variable. import dis def main(): if "take_sum" not in globals(): take_sum = 1 print(take_sum) print(dis.dis(main)) Even if the comparison in line 5 (of the python code) causes execution to skip the assignment in line 6, main() still has a variable named (take_sum). It is part of the function's code.main() creates (take_sum) because line 6 does an assignment to the variable. (take_sum) is a local variable because main() does not declare it as global. Notice how the code changes if we declare take_sum as global. import dis def main(): global take_sum if "take_sum" not in globals(): take_sum = 1 print(take_sum) print(dis.dis(main)) Now main() uses STORE_GLOBAL and LOAD_GLOBAL when referencing (take_sum).To fix your code, you could modify main() to always assign something to take_sum: def take_sum(a, b, c): return a+b+c def main(): if "take_sum" in globals(): take_sum = globals()["take_sum"] else: take_sum = lambda x,y,z: x+y+z print(take_sum) main()Or you could declare take_sum as global def take_sum(a, b, c): return a+b+c def main(): global take_sum if "take_sum" not in globals(): take_sum = lambda x,y,z: x+y+z print(take_sum) main() RE: unbounded variable - akbarza - Feb-07-2024 hi, thanks I read your reply, although I am unfamiliar with dis module.so I did not understand the blue sections of your reply. can I ask you why the error is not created in main1 function (in my Python code)? thanks again RE: unbounded variable - deanhystad - Feb-07-2024 Quote:I am unfamiliar with dis module.so I did not understand the blue sections of your reply.You can read all about the python disassembler here: https://docs.python.org/3/library/dis.html Quote:why the error is not created in main1 functionThere are no assignments in main1, so main1 doesn't make a local variable. This would generate an error def main1(): print(take_sum)If you didn't have a function named "take_sum" in the global namespace. When accessing variables python first looks local, then enclosed, global and finally built-in. |