• 基础DAY11-模块import


    import hm_01_测试模块1
    import hm_02_测试模块2
    hm_01_测试模块1.say_hello()
    dog = hm_01_测试模块1.Dog()
    print(dog)
    hm_02_测试模块2.say_hello()
    cat = hm_02_测试模块2.Cat()
    print(cat)
    print(hm_01_测试模块1.title)
    print(hm_02_测试模块2.title)
    import导入模块
    import hm_01_测试模块1 as DogModule
    import hm_02_测试模块2 as CatModule
    DogModule.say_hello()
    dog = DogModule.Dog()
    print(dog)
    CatModule.say_hello()
    cat = CatModule.Cat()
    print(cat)
    print(DogModule.title)
    print(CatModule.title)
    import 同时指定别名

    from hm_01_测试模块1 import Dog
    from hm_02_测试模块2 import Cat
    # 如果两个模块,存在同名的函数,那么导入模块的函数,会覆盖掉先导入的函数
    from hm_02_测试模块2 import say_hello
    from hm_01_测试模块1 import say_hello
    # 不需要使用模块调用类了
    dog = Dog()
    print(dog)
    cat = Cat()
    print(cat)
    say_hello()
    from import
    from hm_01_测试模块1 import Dog
    from hm_02_测试模块2 import Cat
    # 如果两个模块,存在同名的函数,那么导入模块的函数,会覆盖掉先导入的函数
    # 统一写在顶部,可以使用别名as分别定义同名的函数
    from hm_02_测试模块2 import say_hello as say_hello1
    from hm_01_测试模块1 import say_hello as say_hello2
    # 不需要使用模块调用类了
    dog = Dog()
    print(dog)
    say_hello1()
    say_hello2()

    from import *(不建议使用)
    函数重名没有任何的提示,出现问题不好排查

    import random
    rand = random.randint(1, 10)
    print(random.__file__)
    print(rand)

    C:UsersAdministratorAppDataLocalProgramsPythonPython35lib andom.py

    # 全局变量 函数 类,直接执行的代码不是向外界提供的工具
    def say_hello():
        print("大家好,我是say hello")
    # 如果直接执行模块 得到的值永远是__main__
    if __name__ == "__main__":
        # 文件被导入时,能够直接执行的代码不需要被执行
        print(__name__)
        print("这是小明开发的模块")
        say_hello()
    __name__ == "__main__"

  • 相关阅读:
    自用类库整理之SqlHelper和MySqlHelper
    如何设置root登录(滴滴云)
    linux下载命令wget
    linux下查看已经安装的jdk 并卸载jdk
    Angular之constructor和ngOnInit差异及适用场景(转)
    【Spring Boot-技巧】API返回值去除为NULL的字段
    jackson 实体转json 为NULL或者为空不参加序列化
    Android 将Android项目打包成aar文件
    Linux修改war包中文件
    Android 7.0 Gallery图库源码分析4
  • 原文地址:https://www.cnblogs.com/joycezhou/p/11402771.html
Copyright © 2020-2023  润新知