V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
pla
V2EX  ›  Python

这段意思没看懂,求助

  •  
  •   pla · 2017-10-07 20:29:41 +08:00 · 1926 次点击
    这是一个创建于 2435 天前的主题,其中的信息可能已经有所发展或是发生改变。

    stackoverflow 上关于 What will happen if two modules import each other? 的讨论

    https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python

    这一段没看懂,哪个是哪个看晕了:

    Finally, the executing script runs in a module named __main__, importing the script under its own name will create a new module unrelated to __main__.


    例子

    # first.py
    from second import b
    
    def a():
        print('This is a')
    
    
    # second.py
    from first import a
    
    def b():
        print('This is b')
    
    
    

    执行 second.py

    1 条回复    2017-10-07 20:42:28 +08:00
    geelaw
        1
    geelaw  
       2017-10-07 20:42:28 +08:00   ❤️ 2
    python some_file.py 的时候,some_file.py 就是 the executing script。

    意思是 some_file.py 自己也成为 module,并且这个 module 的名字叫 __main__,如果 some_file.py 直接或者间接地导入 some_file.py 自己,则被导入的 some_file.py 会成为一个新的 module,且这个新的 module 不是原来的 __main__。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2202 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 01:40 · PVG 09:40 · LAX 18:40 · JFK 21:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.