indirect import of standard module

B

BartlebyScrivener

I know this must have been answered a hundred times, but I must be
searching on the wrong terminology.

Let's say I have a module foo.py that imports os.

I make another script called bar.py that imports foo.py and now I want
to use, say, os.walk in bar.py.

Which is faster or more correct or whatever: Do I import os at the top
of bar.py and use foo's functions?

Or do I call os.walk by writing foo.os.walk?

Sorry for lack of proper terminology. I read the module parts of the
Tutorial but couldn't find it there.

Rick
 
A

alisonken1

Unless you override some of os.* functions in foo, you want to import
os into foo and bar separately.

Python does not reimport the module a second time (create a second
instance of os), it only creates a pointer to the first instance that's
loaded.
 
B

BartlebyScrivener

You're right!

When running bar.py, id(os) and id(foo.os) give the same number!

Cool. I'll go read about INSTANCES and pointers.

Thank you very much,

rick
 
B

Ben Finney

alisonken1 said:
Unless you override some of os.* functions in foo, you want to
import os into foo and bar separately.

Python does not reimport the module a second time (create a second
instance of os)

More accurately, it *does* import it twice, into two separate
namespaces; but loads and executes the code only the first time.
 
A

alisonken1

Actually, it does not "execute the code only the first time", more
accurately, it "initializes the code only the first time".

But you are correct, it exposes the os.* module into the current
namespace so you don't have to go to convoluted lengths to get to it.
 
B

BartlebyScrivener

More accurately, it *does* import it twice, into two separate
If it's in two different namespaces, how can they have the same id
number?

rick
 
A

alisonken1

Although 'namespace' may be a misnomer, module interfaces are 'exposed'
to the module that imports it - it's not imported a second time into
the new 'namespace'. The confusion comes about thinking that modules
and classes are related.

When a module is first imported, an instance is created for the module
and the public interfaces/variables are exposed to the module that did
the import. When another call to import the same module from somewhere
else, Python recognizes that the module has already been imported, so
it only creates a reference link to the new module that called import.
That is why you get the same ID number for the module function calls.

It's similar to classes in that Python keeps track of 'instances' of
classes as well as 'instances' of modules, the 'class' instance is
based upon variables which can be either the same class (think of a
call to a function with a class instance as the input) or a new
instance (where the same class is reused, but with different values),
whereas modules are based upon executable bytecode where there is only
one instance at all times during the program execution.
 
B

BartlebyScrivener

Thank you for the elucidation. I'm just getting into Classes, but I
wanted to get modules down first.

Much appreciated.

rick
 
B

Ben Finney

BartlebyScrivener said:
If it's in two different namespaces, how can they have the same id
number?

That "two different namespaces" might be a bit inaccurate.

There is only one object representing the module, which is created the
first time it is imported. That object has a unique id.

There can be any number of names referring to that module object, in
different parts of the namespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top