python3基础篇(十一)——import模块

python3基础篇(十一)——import模块

前言:
阅读这篇文章我能学到什么?
  import关键字用于导入现有的模块,增强了代码的重用性。可以和from和as关键字一起使用。这篇文章将为你介绍import的几种用法。

——如果你觉得这是一篇不错的博文,希望你能给一个小小的赞,感谢您的支持。

1 import [ModularName]

1.1 导入一个模块

  为了实现代码的重用性,方便将来或他人使用,我们通常会将代码进行模块化。而python3中提供了import关键字导入现成的模块,导入的模块即可以是开发者自己实现的,也可以是第三方库。python提供了丰富的第三方库,使得大多数功能都不需要自己去重复实现。
  代码示例:
  TestA.py

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

import TestA

print(TestA.TestA_Variable)         #访问模块TestA的变量

TestA.TestA_Function()              #访问模块TestA的函数

Test = TestA.TestA_Class("Test")    #创建TestA中的类
Test.MyPrint()

1.2 导入多个模块

  import可以一次导入多个模块,通过,隔开。
  代码示例:
  我们创建TestB模块。
  TestB.py

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

import TestA, TestB

#TestA
print(TestA.TestA_Variable)         #访问模块TestA的变量

TestA.TestA_Function()              #访问模块TestA的函数

TA = TestA.TestA_Class("Test")    #创建TestA中的类
TA.MyPrint()

#TestB
print(TestB.TestB_Variable)         #访问模块TestA的变量

TestB.TestB_Function()              #访问模块TestA的函数

TB = TestB.TestB_Class("Test")    #创建TestA中的类
TB.MyPrint()

  运行结果:

TestA Variable
TestA Function
TestA Class. Test
TestB Variable
TestB Function
TestB Class. Test

2 import [ModularName] as [OtherName]

  有时候为了操作方便或命名规范,我们会给模块起别名。as关键字用于给模块起别名。
  代码示例:
  main.py

import TestA as A               #取别名

print(A.TestA_Variable)         #访问模块TestA的变量

A.TestA_Function()              #访问模块TestA的函数

Test = A.TestA_Class("Test")    #创建TestA中的类
Test.MyPrint()

  代码示例:

TestA Variable
TestA Function
TestA Class. Test

  上面讲了import关键字可以一次性导入多个模块,导入多个模块时也可以分别取别名。
  语法结构:

import [ModularName] as [OtherName], [ModularName] as [OtherName]

  代码示例:

import TestA as A, TestB as B

  需要注意的是模块TestATestB分别被取名AB之后,原模块名则不能再使用。

3 from [ModularName] import [MemberName] as [OtherName]

  使用该结构可以指定导入某块内的特定成员,而不必全部引入。
  代码示例:

from TestA import TestA_Variable
from TestA import TestA_Function
from TestA import TestA_Class

#TestA
print(TestA_Variable)         #访问模块TestA的变量

TestA_Function()              #访问模块TestA的函数

TA = TestA_Class("Test")    #创建TestA中的类
TA.MyPrint()

  运行结果:

TestA Variable
TestA Function
TestA Class. Test

  当使用该结构导入某模块指定的成员后,可以直接通过成员名访问外部成员,而不必像import [ModularName]那样,访问成员还需要TestA.TestA_Variable的形式通过模块名.成员名
  当想要导入模块内所有成员时,可以使用*符号。
  代码示例:

from TestA import *

#TestA
print(TestA_Variable)         #访问模块TestA的变量

TestA_Function()              #访问模块TestA的函数

TA = TestA_Class("Test")      #创建TestA中的类
TA.MyPrint()

  from [ModularName] import *形式有一个缺点,当导入多个模块时,如果几个模块中成员存在重名,则会出现后导入的模块将之前导入的同名成员覆盖的情况,而且编译器不会给出任何提示信息。所以建议还是通过模块名去调用模块的成员,防止出现同名覆盖情况。
  代码示例:

from TestA import *
from TestB import *           #覆盖了TestA中更早的TestSame

print(TestA_Variable)         #访问模块TestA的变量

TestA_Function()              #访问模块TestA的函数

TA = TestA_Class("Test")      #创建TestA中的类
TA.MyPrint()

print(TestSame)               #同名成员

  运行结果:

TestA Variable
TestA Function
TestA Class. Test
Test Same. B

  如果交换from TestA import *from TestB import *两行的位置,则输出结果发生变化。
  运行结果:

TestA Variable
TestA Function
TestA Class. Test
Test Same. A

  同样的也可以使用别名,这里就不演示了。

4 嵌套import

  我们知道一个模块可以import导入其他模块,这个模块本身也可以被其他模块导入,这就形成了嵌套import。如果一个模块导入了其他模块,那么这个模块被导入后,也就可以通过这个模块访问其导入的其他模块。
  代码示例:
  TestA.py

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

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

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

from TestA import *

print(TestA_Variable)               #访问模块TestA的变量
print(TestB_Variable)               #访问模块TestA的变量
print(TestC.TestC_Variable)         #访问模块TestA的变量

TestA_Function()                    #访问模块TestA的函数
TestB_Function()                    #访问模块TestA的函数
TestC.TestC_Function()              #访问模块TestA的函数

TA = TestA_Class("Test")            #创建TestA中的类
TB = TestB_Class("Test")            #创建TestA中的类
TC = TestC.TestC_Class("Test")      #创建TestA中的类
TA.MyPrint()
TB.MyPrint()
TC.MyPrint()

  运行结果:

TestA Variable
TestB Variable
TestC Variable
TestA Function
TestB Function
TestC Function
TestA Class. Test
TestB Class. Test
TestC Class. Test
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章