"Garry Willgoose" <garry.willgo...@newcastle.edu.au> wrote
I want to be able to import multiple instances of a module and call each
by a unique name and it doesn't appear at first glance that either import
or __import__ have what I need.
No because it would be bad practice.
Stateful modules lead to hard to debug prolems.
computing platform I have developed where users write python scripts to
do some analysis of science problems where they call modules that have
code for monipulating data and where the imported module does some
specific scientfic manipulation of data. The key problem is that the
module might locally store some partial results ready for the next time
its called to save CPU time
This is where classes come in. You can define a class in
your module with the analysis done in a method and have it
store the results in instance variables.
Then you can either call another method which can access those
variables or you can create another instance of the class with its
own independant copies of its values.
But if the module is called for two different purposes in two different
parts of the script then the second call will actually see the partial
results from the 1st call and vice versa.
Technically you don't call a module you import it which has
the side-effect of executing the code in the module. In a well structured
module the code should really be inside function definitions so
that nothing haoppens until you call the modules functions.
importing modules as a way of executing the code within them
is not good practice.
The simple solution is if it were possible to import the same module
file twice but for them to act like they were different modules. It was
also make it easy to parallelise code without the called module needing
to be thread safe (each instance would be one thread) but that is a
lesser priority for the moment.
Again classes can do that for you. Either by having two instances
of the class in a single thread and multiplexing the calls to their methods
(round-robin style) or, if necessary, by having two instances each
in their own thread executing independantly.
If I have module in a file called stuff.py has in it a global variable
somevariable I want to be able to import multiple instance (with
seperate name spaces etc) of stuff so that I could have for example
a=instance1ofstuff.somevariable
b=instance2ofstuff.somevariable
With a class that becomes:
import stuff
a = stuff.myClass()
b = stuff.myClass()
a.myFunction(x,y,z)
b.myFunction(a,b,c)
print a.myvar, b.myvar
etc.
and a and b are referring to different variables in entirely different
modules. IS there any way to do this?
a and b refer to independant instances of the same class each
with its own myvar variable inside.
See the OOP topic of my tutorial for more on
Object Oriented Programming
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor