Python基础学习(七)-----包与模块管理及面向对象初步

一. 包与模块管理

1. 模块

  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()
  1. from
from models import page

def hello():
    print(page)

hello()
  1. importlib.reload (模块)
import importlib
importlib.reload(models)

2. 包

  1. init

3. 为什么要用

  1. 代码重用
  2. 命名空间
  3. 实现数据或服务共享

4. 步骤

  1. 找到模块文件
  2. 编译为字节码
  3. 运行模块文件

5. 搜索范围

  1. 程序主目录
  2. 环境变量
  3. 标准库
  4. 扩展库

二. 面向对象

1. 步骤

  1. OOA面向对象分析
  2. OOD面向对象设计
  3. OOP面向对象编程

2. 实现

  1. 分析对象特征行为
  2. 写类描述对象模版
  3. 实例化,模拟过程
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()
发布了11 篇原创文章 · 获赞 0 · 访问量 183

猜你喜欢

转载自blog.csdn.net/mangogogo321/article/details/104243021