I am trying to check for where a global file has been defined or not.
import os, sys, logging...
global my_logger
def init():
if my_logger is None:
print 'Logger needs to be initialized'
However, when I run this, I get the error:
NameError: global name 'my_logger' is not defined
Isn’t there a way to check if a variable has been declared or no? Do I have to initialize global variables before I can run a check?
You’re not using the
globalkeyword correctly, look up some documentation concerning that.As a general rule, however, you can test whether a global variable is defined like so: