使用点符号导入
Importing with dot notation
谁能给我解释一下?
当您导入 Tkinter.Messagebox
时,这实际上意味着什么(点表示法)?
我知道您可以导入 Tkinter
但是当您导入 Tkinter.Messagebox
时,这到底是什么?是 class 里面的 class 吗?
我是 Python 的新手,点符号有时让我感到困惑。
import a.b
将 b
导入到命名空间 a
中,您可以通过 a.b
访问它。请注意,这仅在 b
是一个模块时有效。 (例如 Python 3 中的 import urllib.request
)
然而,from a import b
将 b
导入当前命名空间,可由 b
访问。这适用于 类、函数等
使用 from - import 时要小心:
from math import sqrt
from cmath import sqrt
两个语句都将函数 sqrt
导入当前命名空间,但是,第二个导入语句覆盖第一个。
当您将那个点放在您的导入中时,您指的是您从中导入的 package/file 中的内容。
您导入的内容可以是 class、包或文件,每次您放置一个点时,您都会询问它之前的实例中的内容。
parent/
__init__.py
file.py
one/
__init__.py
anotherfile.py
two/
__init__.py
three/
__init__.py
例如你有这个,当你传递 import parent.file
你实际上是在导入另一个 python 模块,它可能包含 classes 和变量,所以要引用一个特定的变量或 class 在那个文件中你做 from parent.file import class
例如。
这可能会更进一步,将包装导入另一个包内或将 class 导入包内的文件等(如 import parent.one.anotherfile
)
有关详细信息,请阅读 Python documentation。
谁能给我解释一下?
当您导入 Tkinter.Messagebox
时,这实际上意味着什么(点表示法)?
我知道您可以导入 Tkinter
但是当您导入 Tkinter.Messagebox
时,这到底是什么?是 class 里面的 class 吗?
我是 Python 的新手,点符号有时让我感到困惑。
import a.b
将 b
导入到命名空间 a
中,您可以通过 a.b
访问它。请注意,这仅在 b
是一个模块时有效。 (例如 Python 3 中的 import urllib.request
)
from a import b
将 b
导入当前命名空间,可由 b
访问。这适用于 类、函数等
使用 from - import 时要小心:
from math import sqrt
from cmath import sqrt
两个语句都将函数 sqrt
导入当前命名空间,但是,第二个导入语句覆盖第一个。
当您将那个点放在您的导入中时,您指的是您从中导入的 package/file 中的内容。 您导入的内容可以是 class、包或文件,每次您放置一个点时,您都会询问它之前的实例中的内容。
parent/
__init__.py
file.py
one/
__init__.py
anotherfile.py
two/
__init__.py
three/
__init__.py
例如你有这个,当你传递 import parent.file
你实际上是在导入另一个 python 模块,它可能包含 classes 和变量,所以要引用一个特定的变量或 class 在那个文件中你做 from parent.file import class
例如。
这可能会更进一步,将包装导入另一个包内或将 class 导入包内的文件等(如 import parent.one.anotherfile
)
有关详细信息,请阅读 Python documentation。