• python import详解


    1.import作用
    引入模块

    2.import的特点
    一个程序中,import的模块不会重复被引用,如:

    # test1.py
    import test2
    print test2.attr
    
    # test2.py
    import test1
    attr = 'hello world'
    
    # test.py
    import test1
    

    运行结果:

    结果分析:
    当执行test.py时,执行import test1语句
    1)判断test1是否在sys.modules中,不在,则创建一个新的module对象test1,放到sys.modules中,然后执行test1.py的其他语句,填充module test1的__dict__属性。
    2)test1.py的第一句是import test2,判断test2是否在sys.modules中,不在,则创建一个新的module对象test2,放到sys.modules中,执行test2.py中的其他语句。
    3)test2.py的第一句是import test1,由于test1模块在sys.modules中有了,所以会跳过这条import,执行test2.py中的其他语句,也就是"attr='hello world'",并放到了test2模块的__dict__属性。
    4)接着会执行test1.py中的其他语句,即"print test2.attr",也就是"hello world"

  • 相关阅读:
    并发编程之进程池,线程池 和 异步回调,协程
    form与modeform
    5个_meta方法
    CRM项目知识预备
    Jason数据库查询语句
    kindeditor编辑器
    几种单例模式
    BBS项目复习
    BBS项目小组件
    BBS项目附加知识
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/7803548.html
Copyright © 2020-2023  润新知