Python 如何引入同级包和模块
作者:程序员很优秀 时间:2022-12-01 02:47:37
如何引入同级包和模块
工程项目结构如下
包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 *
来源:https://blog.csdn.net/weixin_42034322/article/details/81215072
标签:Python,引入,同级包,模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp.net(c#)实现从sqlserver存取二进制图片的代码
2023-06-26 21:48:03
Python sqlite3事务处理方法实例分析
2022-12-28 07:30:09
Golang使用Gin框架实现HTTP上传文件过程介绍
2024-04-28 09:18:12
![](https://img.aspxhome.com/file/2023/1/134001_0s.png)
分享网站群发站内信数据库表设计
2024-01-14 09:54:28
利用Python脚本实现ping百度和google的方法
2022-03-21 06:45:37
Access数据库下如何使用通用对话框的问题
2008-11-28 16:37:00
python为图片和PDF去水印详解
2023-06-28 14:09:43
![](https://img.aspxhome.com/file/2023/7/78597_0s.png)
教你用python控制安卓手机
2023-11-10 10:13:39
![](https://img.aspxhome.com/file/2023/4/69554_0s.png)
python图像常规操作
2022-12-28 18:21:52
![](https://img.aspxhome.com/file/2023/0/83760_0s.jpg)
AMP Tensor Cores节省内存PyTorch模型详解
2021-07-08 01:30:32
![](https://img.aspxhome.com/file/2023/4/88964_0s.jpg)
Python虚拟环境项目实例
2023-03-07 16:59:01
![](https://img.aspxhome.com/file/2023/1/100881_0s.png)
从绘画语言的发展,看视觉设计风格
2008-08-03 17:11:00
Oracle数据库系统使用经验六则
2010-07-26 13:22:00
Python实现基于标记的分水岭分割算法
2023-11-03 08:27:53
![](https://img.aspxhome.com/file/2023/2/66012_0s.jpg)
Python CSV 文件解析和生成方法示例
2022-12-25 10:52:58
TensorFlow tf.nn.max_pool实现池化操作方式
2021-08-20 20:36:45
![](https://img.aspxhome.com/file/2023/1/130541_0s.jpg)
PHP 解决utf-8和gb2312编码转换问题
2024-04-29 13:56:45
浅谈ACCESS数据库升迁SQLSERVER注意事项
2007-08-11 13:44:00
Vue使用Echarts画柱状图详解
2024-05-29 22:22:29
![](https://img.aspxhome.com/file/2023/6/123176_0s.png)
Python 如何实时向文件写入数据(附代码)
2022-11-10 13:40:03
![](https://img.aspxhome.com/file/2023/2/93682_0s.png)