다음 코드를 사용하여 파이썬에 변수가 있는지 확인합니다.
예시
x =10 class foo: g = 'rt' def bar(self): m=6 print (locals()) if 'm' in locals(): print ('m is local variable') else: print ('m is not a local variable') f = foo() f.bar() print (globals()) if hasattr(f, 'g'): print ('g is an attribute') else: print ("g is not an attribute") if 'x' in globals(): print ('x is a global variable')
출력
다음 출력을 얻습니다.
{'self': <__main__.foo instance at 0x0000000002E24EC8>, 'm': 6} m is local variable {'f': <__main__.foo instance at 0x0000000002E24EC8>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:/Users/TutorialsPoint1/~.py', '__package__': None, 'x': 10, '__name__': '__main__', 'foo': <class __main__.foo at 0x0000000002D29828>, '__doc__': None} g is an attribute x is a global variable Process finished with exit code 0