Cross Module Command Useage

Peter Decker pydecker at gmail.com
Sun Mar 12 15:35:21 CET 2006


On 3/12/06, Keith <python at cgcreator.com> wrote:
> So lets say have two modules.. moduleA and moduleB… they are both imported
> into a main python program using the "from module import *" command…

There's your big mistake. This sort of import pollutes the namespace,
because now all the items in the module are no longer linked by their
module name. You're much better off doing a straight 'import module'
command.

> now moduleA has a dynamic command that needs to access a command that is in
> moduleB… but when I run these modules from the main python scrip they cant
> see each other…. it gives me an error that the command does not exist… Dose
> this mean that I need to import moduleB into moduleA for it to see it… or is
> there a way that I can tell moduleA too look out to the main python program
> for the commands…

If you use the import format above, it's simple: if you want to call a
ModuleB command, the syntax is ModuleB.command().

The big advantage of preserving the module namespaces is that if both
ModuleA and ModuleB have a function with a common name, such as
'start()', there is no confusion as to which one you are calling.
Spelling out ModuleA.start() and ModuleB.start() takes care of all
possible ambiguities.

--

# p.d.



More information about the Python-list mailing list