Q:

How to set global variable with method

abc  = None

def load() :
   abc  = cPickle.load('a.pkl')

load()

def main(review):
   print abc.predict('example')  

The variable abc is still set to None.  main is accessing abc many times and I don't want to load the file every time. How can I load the contents of the file once?

A:

With global keyword
abc  = None

def load() :
   global abc
   abc  = cPickle.load('a.pkl')

load()

def main(review):
   print abc.predict('example')  

Without global interpreter will create a new local variable tested in function scope.
But better to use return statement and local variables like
def load() :
   return cPickle.load('a.pkl')

def main(review):
   abc = load()
   print abc.predict('example')

