作者:persisit110 | 来源:互联网 | 2023-05-18 23:44
ImnewtoPythonandprogrammingingeneral(acoupleofweeksatmost).我是Python和编程的新手(一般最多几周)。C
I'm new to Python and programming in general (a couple of weeks at most).
我是Python和编程的新手(一般最多几周)。
Concerning Python and using modules, I realise that functions can imported using from a import *
.
关于Python和使用模块,我意识到可以使用import *导入函数。
So instead of typing
所以不要打字
a.sayHi()
a.sayBye()
I can say
我可以说
sayHi()
sayBye()
which I find simplifies things a great deal. Now, say I have a bunch of variables that I want to use across modules and I have them all defined in one python module. How can I, using a similar method as mentioned above or an equally simple one, import these variables. I don't want to use import a
and then be required to prefix all my variables with a.
.
我觉得它简化了很多东西。现在,假设我有一堆变量,我想在模块中使用它们,我将它们全部定义在一个python模块中。我怎样才能使用上面提到的类似方法或同样简单的方法导入这些变量。我不想使用import a然后被要求在我的所有变量前面添加一个..
The following situation would by ideal:
理想情况如下:
a.py
a.py
name = "Michael"
age = 15
b.py
b.py
some_function
if name == "Michael":
if age == 15:
print("Simple!")
Output:
输出:
Simple!
4 个解决方案
26
Just for some context, most linters will flag from module import *
with a warning, because it's prone to namespace collisions that will cause headaches down the road.
只是对于某些上下文,大多数linters将从模块导入*标记警告,因为它容易发生命名空间冲突,这将导致头痛。
Nobody has noted yet that, as an alternative, you can use the
没有人注意到,作为替代方案,你可以使用
from a import name, age
form and then use name
and age
directly (without the a.
prefix). The from [module] import [identifiers]
form is more future proof because you can easily see when one import will be overriding another.
表格,然后直接使用名称和年龄(没有a。前缀)。 from [module] import [identifier]表单更具未来性,因为您可以轻松查看一个导入何时覆盖另一个导入。
Also note that "variables" aren't different from functions in Python in terms of how they're addressed -- every identifier like name
or sayBye
is pointing at some kind of object. The identifier name
is pointing at a string object, sayBye
is pointing at a function object, and age
is pointing at an integer object. When you tell Python:
另请注意,“变量”与Python中的函数没有区别,因为每个标识符如name或sayBye都指向某种对象。标识符名称指向字符串对象,sayBye指向函数对象,age指向整数对象。当你告诉Python时:
from a import name, age
you're saying "take those objects pointed at by name
and age
within module a
and point at them in the current scope with the same identifiers".
你说的是“在模块a中按名称和年龄指向那些对象,并使用相同的标识符在当前范围内指向它们”。
Similarly, if you want to point at them with different identifiers on import, you can use the
同样,如果要在导入时使用不同的标识符指向它们,则可以使用
from a import sayBye as bidFarewell
form. The same function object gets pointed at, except in the current scope the identifier pointing at it is bidFarewell
whereas in module a
the identifier pointing at it is sayBye
.
形成。指向相同的函数对象,除了在当前作用域中,指向它的标识符是bidFarewell,而在模块a中,指向它的标识符是sayBye。
12
Like others have said,
像其他人说的那样,
from module import *
will also import the modules variables.
还将导入模块变量。
However, you need to understand that you are not importing variables, just references to objects. Assigning something else to the imported names in the importing module won't affect the other modules.
但是,您需要了解您不是导入变量,而是仅导入对象。在导入模块中为导入的名称分配其他内容不会影响其他模块。
Example: assume you have a module module.py
containing the following code:
示例:假设您有一个模块module.py,其中包含以下代码:
a= 1
b= 2
Then you have two other modules, mod1.py
and mod2.py
which both do the following:
然后你有另外两个模块,mod1.py和mod2.py都执行以下操作:
from module import *
In each module, two names, a
and b
are created, pointing to the objects 1
and 2
, respectively.
在每个模块中,创建两个名称a和b,分别指向对象1和2。
Now, if somewhere in mod1.py
you assign something else to the global name a
:
现在,如果mod1.py中的某个位置为全局名称指定了其他内容:
a= 3
the name a
in module.py
and the name a
in mod2.py
will still point to the object 1
.
module.py中的名称a和mod2.py中的名称a仍将指向对象1。
So from module import *
will work if you want read-only globals, but it won't work if you want read-write globals. If the latter, you're better off just importing import module
and then either getting the value (module.a
) or setting the value (module.a= …
) prefixed by the module.
因此,如果您想要只读全局变量,那么从模块导入*将起作用,但如果您想要读写全局变量,它将无法工作。如果是后者,你最好只导入导入模块,然后获取值(module.a)或设置模块前缀的值(module.a = ...)。