本篇我们要讲解的globals()函数,涉及到上面两个知识点,下面我们通过语法、参数、实例进行详细的学习。
1.语法
globals()
2.参数
无
3.返回值
返回全局变量的字典。
4.实例
outerVar="thisisaglobalvariable" print("glocalvariables:") print(globals()) ----- glocalvariables: {'__name__':'__main__','__doc__':None,'__package__':None,'__loader__':<_frozen_importlib_external.SourceFileLoaderobjectat0x0000028F38FD1CF8>,'__spec__':None,'__annotations__':{},'__builtins__':<module'builtins'(built-in)>,'__file__':'C:/Users/szdjy/PycharmProjects/untitled/fun.py','__cached__':None,'outerVar':'thisisaglobalvariable'}
内置函数globals()会以字典类型返回当前位置的全部全局变量(变量名:值)
以上就是python globals函数基本内容的介绍,相信小伙伴们已经对返回全局变量字典有所了解,接下来亲自动手尝试下吧。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)