How does Python importing exactly work?

前端 未结 2 1530
心在旅途
心在旅途 2020-11-28 21:23

I have two specific situations where I don\'t understand how importing works in Python:

1st specific situation:

When I import the same modul

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 22:04

    To answer your first question:

    No, python does not get 'imported' twice. When python loads a module, it checks for the module in sys.modules. If it is not in there, it is put in there, and loaded.

    To answer your second question:

    Modules can define what names they will export to a from camelot import * scenario, and the behavior is to create names for the existing values, not to reference existing variables (python does not have references).

    On a somewhat related topic, doing a from camelot import * is not the same as a regular import.

提交回复
热议问题