Python – Uninitialized value in Python

python

What's the uninitialized value in Python, so I can compare if something is initialized, like:

val

if val == undefined ?

EDIT: added a pseudo keyword.

EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.

Duplicate: Just declaring a variable in Python?

Best Answer

Will throw a NameError exception:

>>> val
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'val' is not defined

You can either catch that or use 'val' in dir(), i.e.:

try:
    val
except NameError:
    print("val not set")

or

if 'val' in dir():
    print('val set')
else:
    print('val not set')