Sure, assign None
to it first:
a = 5
c = None
if a == 7:
c = 10
if c:
print c
None
tests as False
in a boolean context, so if c:
still works as written.
You could also catch the NameError
exception:
try:
print c
except NameError:
pass
or use the globals()
and locals()
functions:
if 'c' in locals():
# in a function
if 'c' in globals():
# outside a function
but that's just plain ugly and unnecessary.