James Percival

My Github Pages material

Last updated : 30th August 2018

Test if a variable exists in python

Sometimes we want to write a snippet which acts only if a variable doesn’t existThis one is fairly straightforward to do using the globals function:

	if 'varname' not in globals():
		varname = 'Here now!'
		# do stuff
		print varname

I wonder if there’s a better way?