# test the __globals__ attribute of a function #by d@CC #on 2024BFeb26 progName="fun_globalsB.py" print("progName:",progName) def foo(): pass return #def end if not hasattr(foo, "__globals__"): print("SKIP testing") raise SystemExit print(type(foo.__globals__)) print(foo.__globals__ is globals()) #add a normal (non-global) variable nmop="a" #add a non-dunder global variable global a123 a123 ="abc" foo.__globals__["barvar"] = a123 print('foo.__globals__["barvar"]:',barvar) #add a non-dunder global constant foo.__globals__["bar"] = 123 print('foo.__globals__["bar"]:',bar) print(globals()) dictA = globals() n=0 try: for pairA in dictA: n+=1 print("n:",n) #print(pairA) #print all global symbols pass except: print("ERROR dict pair changed size") finally: print("done") foo.__globals__["bar"] = None print('foo.__globals__["bar"]:',bar) print("trying to set foo.__globals__ to None") try: foo.__globals__ = None print('foo.__globals__:',bar) except AttributeError: print("AttributeError") finally: print("after AttributeError") print("Issue 01: nmop and a123 are not different in globals()") print("Issue 02: a123 and barvar are not different in globals()") print("end of program") #/fun_globalsB.py