global() 메서드는 현재 글로벌 심볼 테이블의 사전을 반환합니다.
심볼 테이블은 컴파일러에 의해 유지되는 데이터 구조이며 프로그램에 대한 모든 필요한 정보를 포함하고 있습니다.
여기에는 변수 이름, 메서드, 클래스 등이 포함됩니다.
심볼 테이블에는 주로 두 가지 종류가 있습니다.
로컬 심볼 테이블은 프로그램의 로컬 범위와 관련된 모든 정보를 저장하며, locals() 메서드를 사용하여 파이썬에서 액세스합니다.
로컬 범위는 함수 내, 클래스 내 등일 수 있습니다.
마찬가지로 글로벌 심볼 테이블은 프로그램의 글로벌 범위와 관련된 모든 정보를 저장하며, globals() 메서드를 사용하여 파이썬에서 액세스됩니다.
전역 범위에는 클래스 또는 함수와 연관되지 않은 모든 함수, 변수가 포함됩니다.
globals 메서드는 매개 변수를 사용하지 않습니다.
현재 글로벌 기호 테이블의 사전을 반환합니다.
globals()
결과
{'In': ['', 'globals()'], 'Out': {}, '_': '', '__': '', '___': '', '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '__name__': '__main__', '_dh': ['/home/repl'], '_i': '', '_i1': 'globals()', '_ih': ['', 'globals()'], '_ii': '', '_iii': '', '_oh': {}, '_sh': <module 'IPython.core.shadowns' from '/usr/local/lib/python3.5/dist-packages/IPython/core/shadowns.py'>, 'exit': <IPython.core.autocall.ExitAutocall at 0x7fbc60ca6c50>, 'get_ipython': <bound method InteractiveShell.get_ipython of <IPython.core.interactiveshell.InteractiveShell object at 0x7fbc6478ee48>>, 'quit': <IPython.core.autocall.ExitAutocall at 0x7fbc60ca6c50>}
globals()['age'] = 25
print('The age is:', age)
결과
The age is: 25
© 2022 pinfo. All rights reserved.