본문 바로가기
파이썬

파이썬 변수가 Python에 정의되어 있는지 확인

by º기록 2021. 1. 21.
반응형

런타임시 코드의 특정 위치에 변수가 설정되었는지 어떻게 알 수 있습니까? (1) 변수가 조건부로 설정 될 수 있고 (2) 변수가 조건부로 삭제 될 수 있기 때문에 이것이 항상 분명하지는 않습니다. Perl의 defined () 또는 PHP의 isset () 또는 Ruby의 defined? 와 같은 것을 찾고 있습니다.

if condition:
    a = 42

# is "a" defined here?

if other_condition:
    del a

# is "a" defined here?

 

해결 방법

 

try:
    thevariable
except NameError:
    print("well, it WASN'T defined after all!")
else:
    print("sure, it was defined.")

 

참조 페이지 https://stackoverflow.com/questions/1592565

 

 

반응형

댓글