Practice mode Python

What happens if the same module is imported twice in one Python program?

Correct answer D. Its code runs only once; later imports reuse the cached module from sys.modules

Explanation

The import system first checks sys.modules, a cache of modules already loaded. If the module is there, the cached object is returned without executing the file again. To force a fresh execution during development you would use importlib.reload(), which re-executes the module code in place.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.