推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
spotfg
V2EX  ›  Python

请问一下, Python 在导入模块时候,具体执行了什么

  •  
  •   spotfg · Mar 28, 2019 · 1804 views
    This topic created in 2633 days ago, the information mentioned may be changed or developed.
    例如:
    第一次在导入 import re 这个模块时候,是直接将 re 模块内容都编译了一遍吗?
    那么,如果模块中有全局变量的赋值运算,也是这时候执行吗?


    所以就有了以下的疑问,假如程序以下列方式重载 a 文件,是重新编译一次及 a 文件的所有模块?
    module_name = importlib.import_module('.', 'a')
    module_name = importlib.reload(module_name)
    result = module_name.main(params)
    1 replies    2019-03-29 09:42:03 +08:00
    spotfg
        1
    spotfg  
    OP
       Mar 29, 2019
    在其他论坛,有大佬这样说:
    '''
    import 语句所做的:
    执行模块代码,接着生成一个 module 对象,里面包含了模块的变量、函数、类
    '''

    但是,importlib.reload(module_name)后 module 对象是被回收了,还是继续存在内存中
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2741 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 15:05 · PVG 23:05 · LAX 08:05 · JFK 11:05
    ♥ Do have faith in what you're doing.