包与模块管理
- 使用目的
- 代码重用
- 命名空间
- 实现数据共享
- 模块指令
- import
- from
- from 模块名 import 功能名 as 新命名
- 导入后想修改或使用最新的功能:
import importlib
importlib.reload(模块)
- 步骤
- 找到模块文件
- 编译为字节码
- 运行模块文件
- 搜索范围
- 程序主目录
- 环境变量
- 标准库
- 扩展库
- 包
- init 只在被导入的时候被调用一次
面向对象编程
- 目标:尽量符合人的思维的方式去分析问题,思考问题,解决问题
- 步骤
- OOA 面向对象分析
- OOD 面向对象设计
- OOP 面向对象编程
- 实现
- 分析对象特征行为
- 写类描述对象模板
- 实例化,模拟过程
从日常人的思维习惯先分析好对象、属性特征、方法行为后,总结出他们之间的关系,然后写类(把分析对象变成代码)
实例:
class Book:
def __init__(self,title,price,author, publisher,pubdate): #初始化对象
self.title=title
self.price=price
self.author=author
self.publisher=publisher
self.pubdate=pubdate
def __repr__(self): #如何定义对象
return '<图书 {}>.format(self.title)'
def printinfo(self):
print('当前这本书的信息如下:')
print('标题:{}'.format(self.title))
print('定价:{}'.format(self.price))
print('作者:{}'.format(self.author))
print('出版社:{}'.format(self.publisher))
print('出版事件:{}'.format(self.pubdate))
book1 = Book('C#经典',29.9, 'Tom','优品课堂',datetime.date(2016,3,1))
print(book1.title))
book.printinfo()
book1