Feb-06-2024, 12:34 PM
hi
in code:
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
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:
Error:Traceback (most recent call last):
File "D:\akb_python\akb_py_projects\unbounded_variable.py", line 25, in <module>
main()
File "D:\akb_python\akb_py_projects\unbounded_variable.py", line 21, in main
print(take_sum)
UnboundLocalError: local variable 'take_sum' referenced before assignment
'''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