zoukankan      html  css  js  c++  java
  • Python 模块.

    容器 -> 数据的封装

    函数 -> 语句的封装

    类    -> 方法和属性的封装

    模块 -> 模块就是程序

    自己写的模块要和调用程序在同意目录下 , 这样才可以成功调用 . 

    模块的存放 .(__name__=='__main',搜索路径和包)

    当我们将一个个的模块写好之后往往需要测试 或者 有其他的用途 然后我们摘模块里面开始测试 , 例如1 =========== RESTART: C:UsersAdministratorDesktop新建文件夹Temp.py ===========

     1 def c2f(cel):
     2     fah=cel*1.8+32
     3     return fah
     4 
     5 def f2c(fah):
     6     cel=(fah-32)/1.8
     7     return cel
     8 
     9 def test():
    10     print('测试 0摄氏度 = %.2f 华氏度 ' % c2f(0))
    11     print('测试 0华氏度 = %.2f 摄氏度 ' % f2c(0))
    12 
    13 test()

     

     

    2 测试 0摄氏度 = 32.00 华氏度 
    3 测试 0华氏度 = -17.78 摄氏度 
    4 >>> 

    在这里是没问题的 , 然后我们开始调用这个包.

    1 import Temp as T
    2 import time
    3 print('32摄氏度 = %.2f 华氏度' %T.c2f(32))
    4 print('99华氏度 = %.2f 摄氏度' %T.f2c(99))
    5 time.sleep(20)
    1 =========== RESTART: C:UsersAdministratorDesktop新建文件夹jack.py ===========
    2 测试 0摄氏度 = 32.00 华氏度 
    3 测试 0华氏度 = -17.78 摄氏度 
    4 32摄氏度 = 89.60 华氏度
    5 99华氏度 = 37.22 摄氏度

    这时候就出问题了 , 我们调用模块时 , 模块中的测试 居然也运行了 , 这可怎么办呢?这时候我们就要告诉Python该模块是作为程序运行还是模块运行了 . 

    在模块中insert这条语句

     1 def c2f(cel):
     2     fah=cel*1.8+32
     3     return fah
     4 
     5 def f2c(fah):
     6     cel=(fah-32)/1.8
     7     return cel
     8 
     9 def test():
    10     print('测试 0摄氏度 = %.2f 华氏度 ' % c2f(0))
    11     print('测试 0华氏度 = %.2f 摄氏度 ' % f2c(0))
    12 
    13 if __name__=='__main__':  #   在模块中insert这条语句
    14     test()

    在主程序中 __name__是__main__ 主程序的意思 , 如果作为包进入了其他的程序 那么 __name__ 就是该模块的名成了 例如将模块程序 进行如下改动 . 然后在运行一下 模块程序

     1 def c2f(cel):
     2     fah=cel*1.8+32
     3     return fah
     4 
     5 def f2c(fah):
     6     cel=(fah-32)/1.8
     7     return cel
     8 
     9 def test():
    10     print('测试 0摄氏度 = %.2f 华氏度 ' % c2f(0))
    11     print('测试 0华氏度 = %.2f 摄氏度 ' % f2c(0))
    12 
    13 print(__name__) #  在这里进行第二次修改
    14 
    15 if __name__=='__main__':  #   在模块中insert这条语句
    16     test()
    1 =========== RESTART: C:UsersAdministratorDesktop新建文件夹Temp.py ===========
    2 __main__
    3 测试 0摄氏度 = 32.00 华氏度 
    4 测试 0华氏度 = -17.78 摄氏度 
    5 >>> 

    可见在本程序运行的时候 __name__的意思是 __main__ , 然后在其他程序中调用一下该模块试试.

    1 import Temp as T
    2 import time
    3 print('32摄氏度 = %.2f 华氏度' %T.c2f(32))
    4 print('99华氏度 = %.2f 摄氏度' %T.f2c(99))
    5 time.sleep(20)
    1 =========== RESTART: C:UsersAdministratorDesktop新建文件夹jack.py ===========
    2 Temp
    3 32摄氏度 = 89.60 华氏度
    4 99华氏度 = 37.22 摄氏度

    可见显示出来的是 该模块的名称 / 

    下面讲解一下搜索路径 / 石景山搜索 路径是一个列表 如电脑中环境变量的储存一样 . 查看自己python的搜索路径都有那些可以这样进行查看.

    1 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
    2 Type "copyright", "credits" or "license()" for more information.
    3 >>> import sys
    4 >>> sys.path
    5 ['', 'C:\Python3.5\Lib\idlelib', 'C:\Python3.5\lib\site-packages\pip-8.1.2-py3.5.egg', 'C:\Python3.5\python35.zip', 'C:\Python3.5\DLLs', 'C:\Python3.5\lib', 'C:\Python3.5', 'C:\Python3.5\lib\site-packages']
    6 >>> 

    只要你的模块在以上路径之内Python就可以自己搜索到 . Python 建议的模块储存区域是.C:\Python3.5\lib\site-packages (site-packages)只要是这个子目录就行 , 安装的不同可能导致目录的不同 . 

    可以发现python的路径存放是以列表的方法 , 所以我们可以向其中添加自己想存放模块的路径  如果需要添加则进行一下操作 . 

    sys.path.append('存放的路径.') 这样就添加了一个搜索路径. 

    最后说一下包的概念 .

    包(用于将模块进行分类.)

    1 : 创建一个文件夹 , 用于储存相关的模块 , 文件夹的名字就是包的名字.

    2 :在其中创建一个名为 __init__.py的文件内容可以为空(龟腚) /

    3 : 将模块放到包(文件夹)中 ,

     

    1 import Jack.Temp as T
    2 import time
    3 print('32摄氏度 = %.2f 华氏度' %T.c2f(32))
    4 print('99华氏度 = %.2f 摄氏度' %T.f2c(99))
    5 time.sleep(20)

    def c2f(cel):
        fah=cel*1.8+32
        return fah
    
    def f2c(fah):
        cel=(fah-32)/1.8
        return cel
    
    def test():
        print('测试 0摄氏度 = %.2f 华氏度 ' % c2f(0))
        print('测试 0华氏度 = %.2f 摄氏度 ' % f2c(0))
    
    print(__name__) #  在这里进行第二次修改
    
    if __name__=='__main__':  #   在模块中insert这条语句
        test()

    运行结果

  • 相关阅读:
    [导入]Repeater与DataGrid的效率,到底哪个的更好?!(结论很可能和你认为的不一样!)
    开发工具的选择
    在WINDOWS 下删除EISA配置的隐藏分区
    DNN使用升级包升级
    DNN中代码创建用户的CreateUser()方法的疑问
    eWebEditor的数据库连接字符串
    c/c++笔试题目(林锐)
    如何下载网页中的flash文件
    DotNetNuke: System.Security.Cryptography.CryptographicException: Bad Data
    win7安装iis错误解决方法汇总
  • 原文地址:https://www.cnblogs.com/A-FM/p/5683607.html
Copyright © 2011-2022 走看看