zoukankan      html  css  js  c++  java
  • day17作业

    1.简述

    什么是模块

    功能就是函数,py文件就可以被称为模块,py文件中有功能。

    模块有哪些来源

    模块的来源有自定义模块(包含第三方模块),内置模块,被编译DLL文件,包

    模块的格式要求有哪些

    创建一个py文件,将你想要的功能写入这个py文件,通过关键字import 模块名导入到当前文件

    2.定义一个cuboid模块,模块中有三个变量长(long)宽(wide)高(high),数值自定义,有一个返回值为周长的perimeter方法,一个返回值为表面积的area方法

    cuboid.py文件

    long = 10
    wide = 5
    high = 15
    def perimeter():
        return 4*(long+wide+high)
    
    def area(long,wide,high):
        return long*wide*high

     


    3.定义一个用户文件stu1.py,在该文件中打印cuboid的长宽高,并获得周长和表面积,打印出来

    import cuboid
    print(cuboid.long)
    print(cuboid.wide)
    print(cuboid.high)
    print(cuboid.perimeter())
    print(cuboid.area())

    4.在stu2.py文件中导入cuboid模块时为模块起简单别名,利用别名完成第3题中完成的操作

    import cuboid as cc
    print(cc.long)
    print(cc.wide)
    print(cc.high)
    print(cc.perimeter())
    print(cc.area())

    5.现在有三个模块sys、time、place,可以在run.py文件导入三个模块吗?有几种方式?分别写出来

    import time,sys,place
    import sys as s
    import time as t
    import place as p
    from sys import *
    from time import *
    from place import *

    6.结合第2、3、4题完成from...import...案例,完成同样的功能

    from cuboid import long as l,wide as w,high as h,perimeter as p,area as a
    print(l)
    print(w)
    print(h)
    print(p())
    print(a())

    7.比较总结import与from...import...各自的优缺点

    import先将导入的模块读取一遍,开辟模块的相应名称空间,需要调用模块的名称需要用模块名或者别名加.名称获取

    from    import   从模块中将相应的功能加入执行文件空间中,相当于名称保存在当前的执行文件的全局名称。

    import 优点:不于执行文件公用名称空间,不会出现重名现象,缺点:调用不方便

    from  import 优点:获取名称方便,缺点:会出现重名

  • 相关阅读:
    Google黑板报上连载的长文
    sql server2000 数据同步
    sql server 2000 数据同步(2)
    reset sql server express sa password
    Fetion分析之二:服务器地址从何而来——变态的配置文件(转)
    CentOS软件安装血泪经验(转)
    《Unix & Linux 大学教程》(转)
    有关CentOS6的man报错
    linux 命令行学习笔记
    ubuntu 無法掛載ntfs分區
  • 原文地址:https://www.cnblogs.com/msj513/p/9773633.html
Copyright © 2011-2022 走看看