pythonimportinfinite-loop

If you import yourself in Python, why don't you get an infinite loop?


This question is a response to the following SO post:

How do I pickle an object?

In that thread, the OP accidentally imports his own module at the top of the same module. Why doesn't this cause an infinite loop?


Solution

  • Modules are imported only once. Python realizes it already has been imported, so does not do it again.

    See: http://docs.python.org/tutorial/modules.html#more-on-modules