title: Accessing a global deque or data structure from within a function
body: <p>I have a need to create global deques in a main program and use them in functions called from that program.  In my example, I create a deque, "questions", and add two lists to the deque.  I print the deque from the main program to show that it was successfully created and populated.</p>
  
  <p>In the function I attempt to pop the items off the deque "questions" and into a variable, "question" but I get a "NameError: global name 'questions' is not defined"</p>
  
  <p>How can I access the deque in the function?  The commented out "global questions" and "from collections import deque" have also been tried with the same result and all the permutations of including or excluding those lines.</p>
  
  <p>The function is in a module, and the file structure is as follows:</p>
  
  <pre><code>├── problemmain.py
  ├── modules
  │   ├── problemchild.pyc
  │   ├── problemchild.py
  │   ├── __init__.pyc
  │   └── __init__.py
  </code></pre>
  
  <p>problemmain.py:</p>
  
  <pre><code>#!/usr/bin/python
  
  # Send them to the id connector
  from modules.problemchild import problem_child
  from collections import deque
  #idconnector_send(questions,\
  #                 rmdconfig.idconnector['send_dst_hostname'],\
  #                 rmdconfig.idconnector['send_dst_port'])
  
  questions=deque()
  
  questions.append(['item1','item2','item3',['inneritem1','inneritem2','inneritem3','inneritem4','inneritem5','inneritem6']])
  questions.append(['item1','item2','item3',['inneritem1','inneritem2','inneritem3','inneritem4','inneritem5','inneritem6']])
  
  print questions
  
  problem_child()
  
  #idconnector_receive(rmdconfig.idconnector['receive_dst_hostname']\
  #                    rmdconfig.idconnector['receive_dst_port'])
  </code></pre>
  
  <p>modules/__init__.py:</p>
  
  <pre><code>#!/usr/bin/python
  #__all__ = ["readconf",]
  </code></pre>
  
  <p>modules/problemchild.py:</p>
  
  <pre><code>#!/usr/bin/python
  
  def problem_child():
  
      """Determine what's wrong with modules scope and deque
      """
  #    from collections import deque
  #    global questions
      while True:
              #question = questions.pop()
              question = questions.pop()
              print "Question is: "
              print question
              print "----------------------------------------------------"
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>./problemmain.py
  
  
  deque([['item1', 'item2', 'item3', ['inneritem1', 'inneritem2', 'inneritem3', 'inneritem4', 'inneritem5', 'inneritem6']], ['item1', 'item2', 'item3', ['inneritem1', 'inneritem2', 'inneritem3', 'inneritem4', 'inneritem5', 'inneritem6']]])
  Traceback (most recent call last):
    File "./problemmain.py", line 17, in &lt;module&gt;
      problem_child()
    File "/home/justin.h.haynes/rmd/modules/problemchild.py", line 11, in problem_child
      question = questions.pop()
  NameError: global name 'questions' is not defined
  </code></pre>
  
  <p>Trying Aaron's suggestion, and replacing "questions_queue"  I get the following:</p>
  
  <pre><code>$ ./problemmain.py
  Traceback (most recent call last):
    File "./problemmain.py", line 13, in &lt;module&gt;
      modules.questions_queue=deque()
  NameError: name 'modules' is not defined
  </code></pre>