472,345 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Instantiate all objects in a module?

I have my own module, called mystuff.py, which is to be imported for an
app.

The number of classes in the module will vary, so I need a subroutine that
will instantiate all of them as objects, and assign a variable to each,
regardless of what names the classes are.

Then I need to be able to delete these objects from memory completely at
times.

Can someone recommend a subroutine for this?
Aug 15 '06 #1
1 1555
Donald Westfield wrote:
I have my own module, called mystuff.py, which is to be imported for an
app.

The number of classes in the module will vary, so I need a subroutine that
will instantiate all of them as objects, and assign a variable to each,
regardless of what names the classes are.
Perhaps if you could explain what you mean by "instantiate all of them
as objects" .... suppose there are two classes, named Foo and Bar, in
the module; do you want to class objects (Foo and Bar), or do you want
one instance of each class (Foo() and Bar())?

Then what do you mean "assign a variable to each"? Do you mean "bind a
name to each"?

Perhaps it would help if you explained what this magic function should
return, or what effect it should have on the environment, and gave
examples of what you'd write after invoking the magic, like

magic_class_gadget(mystuff)
obj1 = class1()
obj2 = class2()
obj3 = ....
obj1.some_method()
.....

The big problem is that you don't know until you import the module how
many classes there are in it ....

Maybe you need something like this:

class_list = magic_class_gadget(mystuff)
obj_list = [cls() for cls in class_list]
# Note: assumption is that each class's __init__ method has the same
signature.
for obj in obj_list:
obj.some_method()
....
del obj_list
del class_list
del mystuff

In that case you can get the class_list by doing this:
import types
class_list = [v for v in mystuff.__dict__.values() if isinstance(v,
types.TypeType)]

Alternatively, forget about the implementation details for the moment
-- come up a level and tell us what you are trying to achieve, what are
the similarites/differences between the classes, why/how does the
number vary, ...
Then I need to be able to delete these objects from memory completely at
times.
(a) Why? (b) The del statement is your friend.
>
Can someone recommend a subroutine for this?
It is highly unlikely that there is an existing function to do this.

HTH,
John

Aug 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Felix Steffenhagen | last post by:
Hello @ all, i'm a newbie in python and have written a module for computations in a bayesian network. The module can be found at:...
0
by: David Mitchell | last post by:
Hello, Here is a very basic question, but it is frustrating me to no end nonetheless. I have one file called addLink.py. In a method in this...
16
by: gabon | last post by:
Due a big project I would like to create different javascript classes and assign them to divs. But how? :) I know the usage of prototype but given...
9
by: the_grove_man | last post by:
I guess my question can go in two directions. I create applications that run multiple queries against a database. Generally speaking in the past...
0
by: jason | last post by:
i have classic ASP code that is calling a C# class library, which is wrapped for COM interop, and registered in the COM+ MMC. i have written 3...
7
by: Daniel Nogradi | last post by:
What is the simplest way to instantiate all classes that are subclasses of a given class in a module? More precisely I have a module m with some...
4
by: GiBo | last post by:
Hi all, I have a class URI and a bunch of derived sub-classes for example HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the...
16
by: Wayne | last post by:
I've read that one method of repairing a misbehaving database is to save all database objects as text and then rebuild them from the text files. ...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.