Python 如何引入同級包和模塊
更新時間:2022年02月11日 10:02:24 作者:程序員很優(yōu)秀
這篇文章主要介紹了Python 如何引入同級包和模塊,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
如何引入同級包和模塊
工程項目結構如下

包AnimalShow和Class_test是同級包,AnimalShow是父類,Gound,Sea,Sky繼承它,Chicken繼承Gound和Sky
首先是Gound.py引入Animal模塊
如下
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from AnimalShow.Animal import Animals
class Gound_Animal(Animals) :
def __init__(self , name , age , message):
self.age = age
self.name = name
self.message = message
print("Gound_Animal初始化完畢")
def printA(self):
print("name : %s, age : %d, message : %s" % (self.name, self.age, self.message))
def GG(self):
print("我是Gound_Animal獨有方法")Sky和Sea同理
下面是同級包的導入,Chicken引入Gound和Sky,如下,
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from AnimalShow import Gound_Animals,Sky_Animals
class chicken(Gound_Animals.Gound_Animal,Sky_Animals.Sky_Animal):
def __init__(self, name, age, message):
self.age = age
self.name = name
self.message = message
print("chicken初始化完畢")
def printA(self):
print("name : %s, age : %d, message : %s" % (self.name, self.age, self.message))
def CC(self):
print("我是chicken獨有方法")
if __name__ == "__main__":
GA = Gound_Animals.Gound_Animal("陸地動物",10,"我是陸地動物")
CK = chicken("小雞",2,"小雞")
CK.printA()
CK.CC()
CK.GG()
CK.hobby()測試結果

如何導入同級.py文件
導入.py文件的所有方法
(python3導入同級包,包名及調用的方法下面會有條紅線,不要怕,沒關系,運行不會報錯)
from 包名 import *
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python中各類Excel表格批量合并問題的實現思路與案例
在日常工作中,可能會遇到各類表格合并的需求。本文主要介紹了Python中各類Excel表格批量合并問題的實現思路與案例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01

