Вопрос

I have reading Learning to Program Python by Mark Lutz. He writes:

Modules are loaded and run on the first import or from, and only the first. This is on purpose—because importing is an expensive operation, by default Python does it just once per file, per process. Later import operations simply fetch the already loaded module object

So if I have 2 packages a and b and I write the statements:

import a
import b

What does this actually do? Does this not load the package b? Is a imported twice?

Это было полезно?

Решение

I believe you are misreading this statement

Modules are loaded and run on the first import or from, and only the first. This is on purpose

This statement is not saying that it will only run the first import. It's saying that for a given module it will only be imported on the first import that references it. Or in other words Python won't import the same module twice. It has no bearing on importing different modules

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top