English 中文(简体)
我如何在座谈中使用其他模块中的变量?
原标题:How can I use variables in other modules in python?
  • 时间:2012-01-13 19:47:58
  •  标签:
  • python

就是这样。 py:

test = { 1 : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]}
import two
two.example()

这两点。 py:

def example():
    print test[1][5]

你能告诉我,为什么会因以下错误而失败?

NameError: global name  test  is not defined

感谢!

最佳回答

您可以在这一职能中进口

def example():
    from one import test
    print test[1][5]

或你可以测试成一个变量

#one.py
two.example(test)

#two.py
def example(test):
    print test[1][5]
问题回答

在沙尔,所有东西都是物体。 http://www.un.org。

。 因此,在<代码>2中,该编码范围并不广。

因为您的两位。 py doesn t 知道test?

页: 1

test = {1: [1, 2, 3, 4, 5, 6]}
import two
two.example(test)

页: 1

def example(test):
    print test[1][5]

Note: I took the liberty to change your test dict entry from 1 to 1 since you called test[1][5] and not test[ 1 ][5].

每个模块(包括你的文字文本)都有自己的全球变量名称空间(全球符号表)。 可以利用其他模块的变量,但并非易事。 此外,通常更好的办法是作为职能理由予以通过。

我们需要进口。 页: 1 页: 1

两种:

from one import *
print test




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签