0
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?

quamrana
  • 33,740
  • 12
  • 54
  • 68
lucy
  • 3,374
  • 5
  • 24
  • 41

2 Answers2

2

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')
kvorobiev
  • 4,911
  • 4
  • 28
  • 34
0

you can declare a global variable with global myvariable

Astrom
  • 757
  • 7
  • 20