• 包与模块管理


    模块:很多个脚本文件,模块之间可能有联系,互相调用,需要引入代码

    模块:import、from

    main:

    import math #只是一个模块,下面有很多成员
    
    
    def hello():
        print(math.pi)
    
    
    if __name__ == "__main__":
        hello()
    import math #只是一个模块,下面有很多成员,math是python定义好的也可以写自己的
    
    
    def hello():
        f = 3.14159
        print(math.pi)
        print(math.trunc(f))#向左取整
    
    
    
    if __name__ == "__main__":
        hello()

    models

    page = 5 #page变量
    
    def test(): #test方法
        print('models.test()')

    下用main打印moedels里page的值

    import math #只是一个模块,下面有很多成员,math是python定义好的也可以写自己的
    import models
    
    
    def hello():
        print(models.page) #page变量
    
    
    
    if __name__ == "__main__": #在主函数里调用
        hello()

    调用它的方法

    import math #只是一个模块,下面有很多成员,math是python定义好的也可以写自己的
    import models
    
    
    def hello():
        print(models.page) #page变量
    
    
    
    if __name__ == "__main__": #在主函数里调用
        models.test() #调用它的方法,带上此模块

    views

    x = 99
    
    def test():
        print('views.test()')

    调用vies模块

    import math #只是一个模块,下面有很多成员,math是python定义好的也可以写自己的
    import models
    import views
    
    
    def hello():
        print(models.page) #page变量
    
    
    
    if __name__ == "__main__": #在主函数里调用
        views.test() #调用它的方法,带上此模块

    main

    import math #只是一个模块,下面有很多成员,math是python定义好的也可以写自己的
    from models import page#可以只关注想要的信息
    
    
    
    def hello():
        print(page) #page变量
    
    hello()#调用

    想全打出来使用*

    import math #只是一个模块,下面有很多成员,math是python定义好的也可以写自己的
    from models import *
    
    
    
    def hello():
        test() #选取想关注的信息
    
    hello()#调用

    显示指定,避免重名

    import models
    import views
    
    models.test() #指定哪里的test
    from models import test as m_test #给取个别名
    
    from views import test as v_test
    
    m_test()

    使用模块、包的原因:

    代码重用、命名空间、实现数据或服务共享

    步骤:1找到模块文件。2编译为字节码。3运行模块文件

    models:

    page = 5 #page变量,目标打印moedels里page的值
    
    def test(): #test方法
        print('优品课堂 uke.cc')
        print('models.test()')
        print('www.codeclassroom.com')

    用控制台调用(处理导入问题的模块,比如后期更新什么的,再刷就出不来的更新的情况)

    console下,采用importlib模块,可把添加的信息加进去

    In[2]: import models
    In[5]: import importlib #重新载入功能
    In[6]: importlib.reload(models)
    In[7]: models.test()

    需要目录组织:使用包的概念:python package,然后创建个新的python file

    创建product.py

    x = 100
    
    def get_product_list():
        print('产品列表') #认为product是属于a下面的

    面向对象编程:计算机围绕着人的思维走

     封装:把松散的集合起来,降低代码冗余

    title = 'python入门' #定义
    price = 39.00
    author = "peter"
    
    def search_book(title):#函数定义,括号里接收title的关键字,封装
        print('搜索包含书关键字{}的图书'.format(title)) #把title传入
    
    
    book = {
        'title':'python入门',
        'price':'39.00',
        'author':'peter',
        'search_book':search_book
    }#把信息塞入字典表
    
    print(book['title'])#取得字典表某一个键的值
    print(book.get('price',0.0))#看图书定价,没有返回值0.0
    book.get('search_book')('python')#调用book获取键叫searchbook,想象成函数名,搜索图书中包含python的名称

    class Book:#类描述图书信息
        pass
    
    
    
    book = Book() #实例化
    book.title = 'Python入门'
    book.price = 39.00
    book.author = '优品课堂'
    
    print(book.price)
    import datetime
    class Book:#类描述图书信息
       def __init__(self,title,price,author,publisher,pubdate):#定义一个函数,_预定义,构造函数初始化器,主要是初始化函数
           #self的含义自己本身
           self.title = title #图书必要信息
           self.price = price
           self.author = author
           self.publisher = publisher
           self.pubdate = pubdate
    
       def print_info(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))
    book1.print_info()#book1相当于self
    import datetime
    
    class Book:#类描述图书信息
       def __init__(self,
                    title,
                    price=0.0,
                    author='',
                    publisher=None,
                    pubdate=datetime.date.today()):#定义一个函数,_预定义,构造函数初始化器
           #self的含义自己本身
           self.title = title #图书必要信息
           self.price = price
           self.author = author
           self.publisher = publisher
           self.pubdate = pubdate
    
       def print_info(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))
    book1.print_info()#book1相当于self
    
    book2 = Book('Flask 入门到精通') #book写括号执行init函数,后面省略有默认值
    book2.author="优品课堂"
    book2.publisher="清华大学出"
    book2.print_info()

     总结:类的成员定义,写到init里,写一个self自己的某某某等于什么,self表示每一个实例应该有的值

    In[6]: r = range(1,11)#生成一个范围,r并没有列出1-10
    In[7]: r
    Out[7]: range(1, 11)
    In[8]: d = {'a':1,'b':2}
    In[9]: d
    Out[9]: {'a': 1, 'b': 2}
    In[10]: d.keys()
    Out[10]: dict_keys(['a', 'b']) #没有直接显示列表
    In[11]: from main import *#从main导入所有信息
    当前这本书的信息如下:
    标题:c#经典
    定价:29.9
    作者:Tom
    出版社:优品课堂
    出版时间:2016-03-01
    当前这本书的信息如下:
    标题:Flask 入门到精通
    定价:0.0
    作者:优品课堂
    出版社:清华大学出
    出版时间:2019-12-03
    In[2]: from main import Book #把book类导入
    当前这本书的信息如下:
    标题:c#经典
    定价:29.9
    作者:Tom
    出版社:优品课堂
    出版时间:2016-03-01
    当前这本书的信息如下:
    标题:Flask 入门到精通
    定价:0.0
    作者:优品课堂
    出版社:清华大学出
    出版时间:2019-12-03

    定义类

    __repr__表现,交互式提示符下打印了类的实例而不是属性字段,用它实现,如返回字符串,定义对象在交互式提示符下显示
    import datetime
    
    
    class Book:  # 类描述图书信息
        def __init__(self,
                     title,
                     price=0.0,
                     author='',
                     publisher=None,
                     pubdate=datetime.date.today()):  # 定义一个函数,_预定义,构造函数初始化器
            # self的含义自己本身
            self.title = title  # 图书必要信息
            self.price = price
            self.author = author
            self.publisher = publisher
            self.pubdate = pubdate
    
        def __repr__(self):
            return '<图书{}>'.format(self.title)  # 当前自己这本书标题列出
    
        def print_info(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))
    book1.print_info()  # book1相当于self
    
    book2 = Book('Flask 入门到精通')  # book写括号执行init函数,后面省略有默认值
    book2.author = "优品课堂"
    book2.publisher = "清华大学出"
    book2.print_info()
  • 相关阅读:
    Windows Azure Storage (17) Azure Storage读取访问地域冗余(Read Access – Geo Redundant Storage, RA-GRS)
    SQL Azure (15) SQL Azure 新的规格
    Azure China (5) 管理Azure China Powershell
    Azure China (4) 管理Azure China Storage Account
    Azure China (3) 使用Visual Studio 2013证书发布Cloud Service至Azure China
    Azure China (2) Azure China管理界面初探
    Azure China (1) Azure公有云落地中国
    SQL Azure (14) 将云端SQL Azure中的数据库备份到本地SQL Server
    [New Portal]Windows Azure Virtual Machine (23) 使用Storage Space,提高Virtual Machine磁盘的IOPS
    Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())的注意点
  • 原文地址:https://www.cnblogs.com/shirleysu90/p/11975173.html
Copyright © 2020-2023  润新知