학문의 길/머신러닝

python 변수 존재여부

lipnus 2018. 11. 5. 21:33
반응형

o check the existence of a local variable:

if 'myVar' in locals():
  # myVar exists.

To check the existence of a global variable:

if 'myVar' in globals():
  # myVar exists.

To check if an object has an attribute:

if hasattr(obj, 'attr_name'):
  # obj.attr_name exists.


반응형