469,903 Members | 1,610 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

indirect import of standard module

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

Apr 18 '06 #1
9 2380
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.

Apr 18 '06 #2
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

Apr 18 '06 #3
"alisonken1" <al********@gmail.com> writes:
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.

--
\ "God forbid that any book should be banned. The practice is as |
`\ indefensible as infanticide." -- Dame Rebecca West |
_o__) |
Ben Finney

Apr 18 '06 #4
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.

Apr 18 '06 #5
>> More accurately, it *does* import it twice, into two separate
namespaces;


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

rick

Apr 18 '06 #6

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.

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

Much appreciated.

rick

Apr 19 '06 #8
"BartlebyScrivener" <rp*******@gmail.com> writes:
More accurately, it *does* import it twice, into two separate
namespaces;


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.

--
\ "I used to work in a fire hydrant factory. You couldn't park |
`\ anywhere near the place." -- Steven Wright |
_o__) |
Ben Finney

Apr 19 '06 #9
BartlebyScrivener wrote:
More accurately, it *does* import it twice, into two separate
namespaces;


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


they're mixing up the terminology. each loaded module has exactly one
namespace, no matter how many times you import it.

the modules you're importing to have their own namespaces, of course.

this link may help:

http://effbot.org/zone/import-confus...does-python-do

</F>

Apr 19 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Petri Savolainen | last post: by
2 posts views Thread by Marc Ederis | last post: by
6 posts views Thread by Sean Berry | last post: by
reply views Thread by vincent Salaun | last post: by
14 posts views Thread by Jay O'Connor | last post: by
3 posts views Thread by Andy Leszczynski | last post: by
10 posts views Thread by Jia Lu | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.