简体   繁体   English

“从模块导入*”到底是什么意思?

[英]What exactly does “ from module import * ” mean?

I thought it meant everything in the module. 我认为这意味着模块中的所有内容。 But in tkinter I would have to specifically import things like messagebox, colorchooser and filedialog despite having a "from tkinter import *" command. 但是在tkinter中,尽管有“ from tkinter import *”命令,我还是必须专门导入诸如消息框,colorchooser和filedialog之类的东西。 So exactly what does "import *" mean? 那么“ import *”到底是什么意思?

A module can define an __all__ variable: a list containing the names that will be imported when you do from module import * . 模块可以定义__all__变量:一个包含名称的列表,当您from module import *执行操作时,这些名称将被导入。 Anything not in this list will not actually be imported. 此列表中未包含的所有内容均不会实际导入。 https://docs.python.org/2/tutorial/modules.html https://docs.python.org/2/tutorial/modules.html

这只是意味着您以某种方式导入所有(方法,变量等),因此在使用它们时无需为它们加上前缀。

from module import *

This imports all names except those beginning with an underscore (_). 这将导入除以下划线(_)开头的名称以外的所有名称。

read more: Python modules 阅读更多: Python模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM