473,598 Members | 2,844 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 1619
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
2209
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
1243
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
2178
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
2627
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" dat1.refresh
0
1162
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 object from the ASP application i get "Error '80070002'" which might mean "file not found," based on...
7
2070
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
1677
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. For example uri=URI('http://abcd/...') will make 'uri' an instance of HttpURI class, not instance of...
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 player3 As New Persons.Players ....
16
7181
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 SaveFormsAsText SaveReportsAsText
0
7981
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8284
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8046
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.