473,544 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1616
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 "instantiat e 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_gad get(mystuff)
obj1 = class1()
obj2 = class2()
obj3 = ....
obj1.some_metho d()
.....

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_gad get(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
2204
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 In this module i define four classes. - cdp (conditional probability ) consisting of cdp_entry
0
1238
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 method from that class. Here is the code: def getCategories(): # instantiate the DataUtil class
16
2167
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 newDiv(){...} newDiv.prototype=new div(); and of course it isn't. How to instantiate that class? var newDiv_instance=document.createElement("div");
9
2625
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) making it invisible and have used this whenever i needed it. dat1.databasename = app.path & "mydatabase.mdb" dat1.recordsource = "my query"...
0
1159
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 of them can be instantiated and used by the ASP classic application. however, i've just added a new object, and when i try to instantiate that...
7
2065
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 ): pass
4
1674
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 however maps very well to this example). Now I want to pass a string to constructor of URI() and get an instance of one of the subclasses back....
4
3657
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 in advance knowing how many objects (employee1, employee2, etc) Dim player1 As New Persons.Players Dim player2 As New Persons.Players Dim...
16
7164
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 Fairfield to accomplish the first step: Private Sub SaveObjectsAsText() path = CurrentProject.path & "\ObjectsAsText\" SaveDataAccessPagesAsText...
0
7420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7602
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7704
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4911
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3407
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1837
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
983
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
657
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.