0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonで変数が存在しているかどうかは`locals()`で判断すればOK

Posted at

pythonで変数が存在しているかどうかはlocals()で判断すればOK

test.py
## in
if 'num' in locals():
    print('param exist')
else:
    print('param not exist')

## not in
if 'num' not in locals():
    print('param not exist')
else:
    print('param exist')

実行結果

$ python test.py 
param not exist
param not exist

参考

0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?