zoukankan      html  css  js  c++  java
  • 基础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__"

  • 相关阅读:
    二分图最大匹配的König定理及其证明
    HDOJ 2389 Rain on your Parade
    HDOJ 1083 Courses
    HDOJ 2063 过山车
    POJ 1469 COURSES
    UESTC 1817 Complete Building the Houses
    POJ 3464 ACM Computer Factory
    POJ 1459 Power Network
    HDOJ 1532 Drainage Ditches
    HDU 1017 A Mathematical Curiosity
  • 原文地址:https://www.cnblogs.com/joycezhou/p/11402771.html
Copyright © 2011-2022 走看看