Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unbounded variable
#1
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:
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
Reply


Messages In This Thread
unbounded variable - by akbarza - Feb-06-2024, 12:34 PM
RE: unbounded variable - by deanhystad - Feb-06-2024, 02:00 PM
RE: unbounded variable - by akbarza - Feb-07-2024, 11:58 AM
RE: unbounded variable - by deanhystad - Feb-07-2024, 03:51 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020