|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
How to create a list of objects
Suppose I have a class Module.class
I want to create a list of objects, so I do list = [] foreach ... myinst = Module.class() list.append(myinst) foreach obj in list: obj.doSomething() Is there a problem with this? |
|
#2
|
|||
|
|||
|
No problem at all, except that you need to change 'foreach' to 'for'. You will also need to change some of the names, since 'class' is a reserved word and 'list' is the name of a builtin type.
The basic concept is sound though. Lists can hold any type of object, including class instances. Dave - The Developers' Coach |
|
#3
|
||||
|
||||
|
In an effort to stay intouch during my travels heres an example
...Code:
objects = [] for each in range(10): objects.append(module.Class) This is about as basic as youre going to get; note the capital at the beginning of class. this is done to avoid the problems of key words in names as coach staited. Anyway you should end up with a list 'objects' with 10 new instances of module.Class in it. It may also be possiable for you to do something like this... Code:
objects = [] objects.append(module.Class) * 10 Note: As i can't test anything right now no promises. You might end up with a list containting referances to the same instance (not what you want). You can also do the same thing in less space using list comprehensions in Python 2.3+. Code:
objects = [module.Class for each in range(10)] Hope you find this of some help... Mark. |
|
#4
|
|||
|
|||
|
Quote:
Mark, you forgot to call the Class object to create an instance. The code creates a list of class objects instead of class instances. The correct code is: Code:
objects = [] for each in range(10): objects.append(module.Class()) #or objects = [module.Class() for each in range(10)] The second example you gave: Code:
objects = [] objects.append(module.Class) * 10 Doesn't work at all. Regards, Dave - The Developers' Coach |
|
#5
|
||||
|
||||
|
Hehehhe, damn. Totaly missed that, your right this is only storing the object not an instance of this. Guess thats what you get for not testing stuff out - unfortunatly nothing i can really do about that right now so.
The reason i included the * 10 bit was i was half remembering a post from a few months ago where * was used to populate a list and or dict i believe. Although i since remcalled it was more like this and didnt work anyway .Code:
objects = [module.Class()] * 10 Note: again, untested. Can you tell its been a while? LOL, i should try and get on more but since i'm traveling atm i have to go to these cyber cafes and that costs ![]() Anyway take care, Mark. |
|
#6
|
|||
|
|||
|
Quote:
You are right, it doesn't work, at least not in the way that the original poster wants. Instead of making a list with ten different objects, it will make a list with ten instances of the same object. Enjoy your holiday, Mark (unless you are working, in which case enjoy that instead). Dave - The Developers' Coach |
|
#7
|
||||
|
||||
|
Thanks Dev, am trying my hardest to keep up with you guys bit not enough time and def not enough money. This is kinda a working holiday though so, will have a job at some point
. Laters mate, Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to create a list of objects |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|