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
需要注意的是模塊TestA
和TestB
分別被取名A
和B
之後,原模塊名則不能再使用。
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