Re: Cross Module Command Useage

2006-03-13 Thread Terry Hancock
On Sun, 12 Mar 2006 21:50:32 +0800
Keith [EMAIL PROTECTED] 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. 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. 

Yes, you need for moduleA to import moduleB before
it can do that.

That's not as bad as it sounds, because once you've imported
a module once, all the work is done -- later imports do
nothing except grab an existing namespace for you.

The one caveat that you get into, is don't try to design
two friend modules that import each other.  This results
in an impossible situation (it's recursive).

If you have something that two modules both need, and
a module which needs both of those modules, you should
break the functionality up into four modules, e.g.:

   common_utils.py
   /\
   modA  modB
   \/
  main_mod

I find this is a pretty common pattern for me --
there are some utilities that I write to use throughout
my code, so I put them in one module (or sub-package),
usually named utility or constants which are
imported by all modules, and then there's usually a
top or main module which drives everything else.

Cheers,
Terry

-- 
Terry Hancock ([EMAIL PROTECTED])
Anansi Spaceworks http://www.AnansiSpaceworks.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Cross Module Command Useage

2006-03-12 Thread Keith








Ok so Im new to the python programming language
and this is my first post to this mailing list so here it is



So lets say have two modules.. moduleA and moduleB
they are both imported into a main python program using the from module
import * 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 



I would hate to have to import lets say the socket
module into every module rather than just having it look to the main module for
the commands










-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Cross Module Command Useage

2006-03-12 Thread Peter Decker
On 3/12/06, Keith [EMAIL PROTECTED] 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.
-- 
http://mail.python.org/mailman/listinfo/python-list