从函数内访问全局双端队列或数据结构

时间:2014-03-20 17:42:49

标签: python function scope global deque

我需要在主程序中创建全局deques,并在从该程序调用的函数中使用它们。在我的例子中,我创建了一个deque," questions",并在双端队列中添加了两个列表。我从主程序中打印deque,以显示它已成功创建并填充。

在该功能中,我尝试将项目从#deque"问题"变成一个变量"问题"但我得到一个" NameError:全球名称'问题'未定义"

如何在函数中访问双端队列?评论出的全球问题"和#34;来自集合import deque"我们也尝试过相同的结果以及包含或排除这些行的所有排列。

该功能在一个模块中,文件结构如下:

├── problemmain.py
├── modules
│   ├── problemchild.pyc
│   ├── problemchild.py
│   ├── __init__.pyc
│   └── __init__.py

problemmain.py:

#!/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'])

模块/ __ INIT __ PY:

#!/usr/bin/python
#__all__ = ["readconf",]

模块/ problemchild.py:

#!/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 "----------------------------------------------------"

输出:

./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 <module>
    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

尝试Aaron的建议,并替换&#34; questions_queue&#34;我得到以下内容:

$ ./problemmain.py
Traceback (most recent call last):
  File "./problemmain.py", line 13, in <module>
    modules.questions_queue=deque()
NameError: name 'modules' is not defined

3 个答案:

答案 0 :(得分:0)

为什么不把它粘贴在一个公共名称空间中?

modules.questions_queue=deque()

然后像这样访问它:

modules.questions_queue.append(['item1'...

修改

你需要

import modules

import modules.something

答案 1 :(得分:0)

我建议你让questions成为一个明确的论点:

def problem_child(questions):

并从调用方传递:

print questions
problem_child(questions)

通常情况下,您认为需要使用global变量是一个不好的迹象。

答案 2 :(得分:0)

@jonrsharpe已经回答了这个问题,但我认为在思考过程中进一步探讨为什么这是答案是有建设性的,我认为将问题编辑为用户可能不合适。尝试做同样的事情就找不到原来的例子。

这个问题的精神真的是&#34;我如何在全球范围内访问deque&#34;但是在这里我将解释当我在上面提出更具体的问题时我的假设出了什么问题。这个问题仍然有效 - 只是基于错误的假设和理解。

函数不应该修改全局变量(或者真正的全局变量)。这样做的原因还不是很多。使用变量,列表和&#34;大多数其他类型&#34;这里第9.1节(http://docs.python.org/2/tutorial/classes.html)中提到这是真的。

然而,并非所有对象都是如此。当一个人通过一个双端队列时,只有参考通过(如果我过度简化,有人可能会纠正我)。我理解这一点的方式是引用只是引用同一对象的命名空间中的另一个指针式构造。在传递deque的情况下,它现在在函数内部和全局空间中引用它。

所以我可以创建一个双端队列,添加一些元素,将它传递给一个函数,将元素添加到我在函数中传递的双端队列,然后在函数外部查看内容并证明我一直在使用相同的对象:

#!/usr/bin/python


print "-------- strings --------"
a = "this is something"

print a

def scopetest(b):
    print b + ":was passed to me"
    print "I will change it now"
    b="This is something else"
    print "now it is:"
    print b


scopetest(a)

print a


print "------- deque ----------"

from collections import deque

my_deque = deque()

print "I just made a deque called my_deque"
print "it has:"
print my_deque

my_deque.append("this is the first thing in my deque")
my_deque.append("this is the second thing in my deque")

print "I just appended a couple of lines"
print "it has now:"
print my_deque


print "now I will define a new function called scopetest_deque()"

def scopetest_deque(a):
    a.append("this is the third thing in my deque")
    a.append("this is the fourth thing in my deque")
    print "I just appended a couple of lines"
    print "it has now:"
    print a


print "Calling it now:"
scopetest_deque(my_deque)

print "it has now:"
print my_deque

在输出中我们看到字符串是函数的本地字符串,而在deque中我们只有一个不同的名称,我们在程序的主要部分和函数内修改了相同的deque实例:

./ test.py

-------- strings --------
this is something
this is something:was passed to me
I will change it now
now it is:
This is something else
this is something
------- deque ----------
I just made a deque called my_deque
it has:
deque([])
I just appended a couple of lines
it has now:
deque(['this is the first thing in my deque', 'this is the second thing in my deque'])
now I will define a new function called scopetest_deque()
Calling it now:
I just appended a couple of lines
it has now:
deque(['this is the first thing in my deque', 'this is the second thing in my deque', 'this is the third thing in my deque', 'this is the fourth thing in my deque'])
it has now:
deque(['this is the first thing in my deque', 'this is the second thing in my deque', 'this is the third thing in my deque', 'this is the fourth thing in my deque'])