一. 包与模块管理
1. 模块
- import
import math
def hello():
print('hello class')
f=3.14159
print(math.pi)
print(math.trunc(f))
if __name__ =='__main__':
hello()
调用其他模块
import models
def hello():
print(models.page)
if __name__ =='__main__':
models.test()
- from
from models import page
def hello():
print(page)
hello()
- importlib.reload (模块)
import importlib
importlib.reload(models)
2. 包
- init
3. 为什么要用
- 代码重用
- 命名空间
- 实现数据或服务共享
4. 步骤
- 找到模块文件
- 编译为字节码
- 运行模块文件
5. 搜索范围
- 程序主目录
- 环境变量
- 标准库
- 扩展库
二. 面向对象
1. 步骤
- OOA面向对象分析
- OOD面向对象设计
- OOP面向对象编程
2. 实现
- 分析对象特征行为
- 写类描述对象模版
- 实例化,模拟过程
mport datetime
class Book:
def __init__(self,title,price,author,publisher,pundate=datetime.date.today()): #self自身的东西等于传过来的东西
self.title = title
self.price = price
self.author = author
self.publisher = publisher
self.pubdate = pundate
def __repr__(self): #定义对象在交互式提示符下显示方式
return '<book {} at {}>'.format(self.title,id(self))
def print_info(self):
print('current information following:')
print('title:{}'.format(self.title))
print('price:{}'.format(self.price))
print('author:{}'.format(self.author))
print('publisher:{}'.format(self.publisher))
print('pundate:{}'.format(self.pubdate))
book1 = Book('C#',29.9,'Tom','class',datetime.date(2016,3,1)) #实例化
book1.author = 'other'
book1.print_info()