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
  • 37,849
  • 12
  • 53
  • 71
lucy
  • 4,136
  • 5
  • 30
  • 47

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
  • 5,012
  • 4
  • 29
  • 35
0

you can declare a global variable with global myvariable

Astrom
  • 767
  • 5
  • 20