title: Flask-Restful: Store Variables in Global Scope Only When Function is Called
body: <p>I'm working on a custom API using flask-restful to extend some functionality for a Knack app I'm building. One of my API endpoints, defined in a file called <code>announcements.py</code>, contains a number of functions which each require a full list of data from Knack's API (get all records, etc).</p>
  <p>I have a separate file, <code>knack_api.py</code> which contains functions to retrieve data from Knack's API based on the parameters I pass. I import this file at the beginning of the endpoint I'm working on right now, so I can call these functions as needed.</p>
  <p>Many of the functions in <code>announcements.py</code> reference the same datasets from Knack. For example, I need to reference a list of all records in my 'Events' object in 3-4 different functions. I could call <code>knack_api.get_all_announcements()</code> each time, but that seems like a waste of resources and API calls when I could just load it once and be done.</p>
  <p>Problem is, if I try to define a constant variable like <code>ALL_EVENTS</code> in the global scope of my <code>announcements.py</code>, the API calls run as soon as I run my Flask app and won't catch any changes made. On the other hand, if I wait to create an <code>all_events</code> variable in the <code>main</code> function of <code>announcements.py</code>, then I have to pass this variable as an argument into every function that needs it.</p>
  <p>Is there any way around this? Or should I just add the arguments to the functions that need them? I know creating global variables is not good... but this feels like an example where it might make sense, no?</p>