473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 1605
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: http://www.informatik.uni-freiburg.de/~steffenh/bayes.py ...
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 file I am trying to instantiate a class and call a...
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 that this could be possible: function...
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 I have used a Data Control (calling it dat1)...
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 objects for the class library so far, and all three...
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 content: # m.py class A: pass class x( A ):...
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 module urllib & friends, however my actual problem...
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 several instances (James, Gunner, etc.), without...
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. I've used the following code posted by Lyle...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.