Python3基础篇(十一)——import模块
前言:
阅读这篇文章我能学到什么?
import关键字用于导入现有的模块,增强了代码的重用性。可以和from和as关键字一起使用。这篇文章将为你介绍import的几种用法。
1 import [ModularName]
1.1 导入一个模块
为了实现代码的重用性,方便将来或他人使用,我们通常会将代码进行模块化。而Python3中提供了import
关键字导入现成的模块,导入的模块即可以是开发者自己实现的,也可以是第三方库。python提供了丰富的第三方库,使得大多数功能都不需要自己去重复实现。
代码示例:
TestA.py
1 2 3 4 5 6 7 8 9 10 11
| TestA_Variable = "TestA Variable"
class TestA_Class: def __init__(self, Variable): self.Variable = Variable
def MyPrint(self): print(f"TestA Class. {self.Variable}")
def TestA_Function(): print("TestA Function")
|
main.py
1 2 3 4 5 6 7 8
| import TestA
print(TestA.TestA_Variable)
TestA.TestA_Function()
Test = TestA.TestA_Class("Test") Test.MyPrint()
|
1.2 导入多个模块
import
可以一次导入多个模块,通过,
隔开。
代码示例:
我们创建TestB模块。
TestB.py
1 2 3 4 5 6 7 8 9 10 11
| TestB_Variable = "TestB Variable"
class TestB_Class: def __init__(self, Variable): self.Variable = Variable
def MyPrint(self): print(f"TestB Class. {self.Variable}")
def TestB_Function(): print("TestB Function")
|
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import TestA, TestB
print(TestA.TestA_Variable)
TestA.TestA_Function()
TA = TestA.TestA_Class("Test") TA.MyPrint()
print(TestB.TestB_Variable)
TestB.TestB_Function()
TB = TestB.TestB_Class("Test") TB.MyPrint()
|
运行结果:
1 2 3 4 5 6
| TestA Variable TestA Function TestA Class. Test TestB Variable TestB Function TestB Class. Test
|
2 import [ModularName] as [OtherName]
有时候为了操作方便或命名规范,我们会给模块起别名。as
关键字用于给模块起别名。
代码示例:
main.py
1 2 3 4 5 6 7 8
| import TestA as A
print(A.TestA_Variable)
A.TestA_Function()
Test = A.TestA_Class("Test") Test.MyPrint()
|
运行结果:
1 2 3
| TestA Variable TestA Function TestA Class. Test
|
上面讲了import
关键字可以一次性导入多个模块,导入多个模块时也可以分别取别名。
语法结构:
1
| import [ModularName] as [OtherName], [ModularName] as [OtherName]
|
代码示例:
1
| import TestA as A, TestB as B
|
需要注意的是模块TestA
和TestB
分别被取名A
和B
之后,原模块名则不能再使用。
3 from [ModularName] import [MemberName] as [OtherName]
使用该结构可以指定导入某块内的特定成员,而不必全部引入。
代码示例:
1 2 3 4 5 6 7 8 9 10 11
| from TestA import TestA_Variable from TestA import TestA_Function from TestA import TestA_Class
print(TestA_Variable)
TestA_Function()
TA = TestA_Class("Test") TA.MyPrint()
|
运行结果:
1 2 3
| TestA Variable TestA Function TestA Class. Test
|
当使用该结构导入某模块指定的成员后,可以直接通过成员名访问外部成员,而不必像import [ModularName]
那样,访问成员还需要TestA.TestA_Variable
的形式通过模块名.成员名
。
当想要导入模块内所有成员时,可以使用*
符号。
代码示例:
1 2 3 4 5 6 7 8 9
| from TestA import *
print(TestA_Variable)
TestA_Function()
TA = TestA_Class("Test") TA.MyPrint()
|
from [ModularName] import *
形式有一个缺点,当导入多个模块时,如果几个模块中成员存在重名,则会出现后导入的模块将之前导入的同名成员覆盖的情况,而且编译器不会给出任何提示信息。所以建议还是通过模块名去调用模块的成员,防止出现同名覆盖情况。
代码示例:
1 2 3 4 5 6 7 8 9 10 11
| from TestA import * from TestB import *
print(TestA_Variable)
TestA_Function()
TA = TestA_Class("Test") TA.MyPrint()
print(TestSame)
|
运行结果:
1 2 3 4
| TestA Variable TestA Function TestA Class. Test Test Same. B
|
如果交换from TestA import *
和from TestB import *
两行的位置,则输出结果发生变化。
运行结果:
1 2 3 4
| TestA Variable TestA Function TestA Class. Test Test Same. A
|
同样的也可以使用别名,这里就不演示了。
4 嵌套import
我们知道一个模块可以import
导入其他模块,这个模块本身也可以被其他模块导入,这就形成了嵌套import
。如果一个模块导入了其他模块,那么这个模块被导入后,也就可以通过这个模块访问其导入的其他模块。
代码示例:
TestA.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from TestB import * import TestC
TestA_Variable = "TestA Variable"
class TestA_Class: def __init__(self, Variable): self.Variable = Variable
def MyPrint(self): print(f"TestA Class. {self.Variable}")
def TestA_Function(): print("TestA Function")
|
TestB.py
1 2 3 4 5 6 7 8 9 10 11
| TestB_Variable = "TestB Variable"
class TestB_Class: def __init__(self, Variable): self.Variable = Variable
def MyPrint(self): print(f"TestB Class. {self.Variable}")
def TestB_Function(): print("TestB Function")
|
TestC.py
1 2 3 4 5 6 7 8 9 10 11
| TestC_Variable = "TestC Variable"
class TestC_Class: def __init__(self, Variable): self.Variable = Variable
def MyPrint(self): print(f"TestC Class. {self.Variable}")
def TestC_Function(): print("TestC Function")
|
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| from TestA import *
print(TestA_Variable) print(TestB_Variable) print(TestC.TestC_Variable)
TestA_Function() TestB_Function() TestC.TestC_Function()
TA = TestA_Class("Test") TB = TestB_Class("Test") TC = TestC.TestC_Class("Test") TA.MyPrint() TB.MyPrint() TC.MyPrint()
|
运行结果:
1 2 3 4 5 6 7 8 9
| TestA Variable TestB Variable TestC Variable TestA Function TestB Function TestC Function TestA Class. Test TestB Class. Test TestC Class. Test
|