English 中文(简体)
1. 模块和全球变量的进口
原标题:Python imports across modules and global variables

我的问题似乎相当基本,但我似乎无法在任何地方找到任何帮助。

file_a.py >>

from xyz import XYZ
class A:
    .
    .
    .

file_b.py >>

import file_a
from file_a import A
class B(A):
    def __init__(self):
        A.__init__(self)

    def someMethod(self):
        XYZ.doSomething()

XYZ.doSomething() fails saying NameError: name XYZ is not defined Even standard imports like import sys done from file_a does not seem to render it usable in file_b. I assumed that should work. Is my understanding wrong? If yes, then is there a way to have common imports and global variables across files? (If it is of nay help, I ve been a C++ and java programmer and am now starting to use python. )

最佳回答

我的理解是否是错误的?

是的,因为从文件_a import A到进口的“A,进入.file_b 。 <代码>file_a <>/code>的名称仅剩下。 如果不是这样的话,那么两个辛迪加就没有什么意义:

import modulename
from modulename import something

如果你的想法是正确的,那么,在第二种形式之后,你总是能够使用<条码>。

如果是,那么是否有办法在档案中实现共同进口和全球变量?

是的,有星号<代码>*的操作者:

from modulename import *

但这带来了名称空间污染问题,例如,从文件_a import * 进口到file_b。 并且,在<代码>上所做的所有进口产品——a。 页: 1 我相信我会这样做!

When for some reason from module import * is needed, a workaround to namespace pollution is to define in module the variable __all__, which whitelists what should be imported with the star operator.

HTH!

问题回答

当你进口一个模块时,该模块中确定的所有变量都可以在其名称空间上获得。 因此,如果<代码>XYZ,可在file_a上查阅。 页: 1 页: 1

The general idea here is that your namespace shouldn t be cluttered with the contents of other namespaces.

是的,你们的理解是错误的。 每个单元都有自己的名称空间,只有你在该档案中明确进口的物品可以在这个名称空间获得。

Contrary to other answers, it is not particularly Pythonic to refer to file_a.XYZ, although this will work. Instead, you should import XYZ and sys at the top of file_b.

import file_a
from file_a import A

I think the problem is: you do not import XYZ really!

from fila_a import * 

它可以解决你的问题,但这不是一种好的办法。

页: 1

from file_a import XYZ

是做的?





相关问题
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 ]="...

热门标签