user3058846
user3058846

Reputation:

Import in python, two modules sharing common resource

For instance, consider 3 modules namely "apple", "orange" and "fruit". Module "apple" imports "orange" and "fruit". Module "orange" imports "fruit" only. Since "fruit" is common for both, could this be done in a different way? Is this inefficient in terms of memory usage and speed?

I wonder how this is done in professionally distributed packages. Say, if a standard library module (viz. httplib) is needed throughout various modules that has GUI code and other complicated stuff. importing this module in every GUI file would be impractical, wouldn't it?

Upvotes: 1

Views: 164

Answers (1)

Steve Barnes
Steve Barnes

Reputation: 28380

Modules that are imported more than one are generally only initialized once and the namespace is introduced into the scope of the module. So in your example above there is one class fruit and the two classes that inherit from it and if you were to introduce 3 varieties of apple there would still only be one underlying fruit class.

This is how professional packages do it. In other languages like C/C++ you need to use wards to prevent multiple imports python does if for you.

Upvotes: 1

Related Questions