SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 9th, 2004, 04:52 AM
omit98 omit98 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 9 omit98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 57 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old April 9th, 2004, 06:35 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,195 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 34 m 6 sec
Reputation Power: 252
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

Reply With Quote
  #3  
Old April 12th, 2004, 10:27 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #4  
Old April 13th, 2004, 04:51 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,195 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 34 m 6 sec
Reputation Power: 252
Quote:
Originally Posted by netytan
In an effort to stay intouch during my travels heres an example ...

Code:
objects = []
for each in range(10): objects.append(module.Class)

...
Mark.


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

Reply With Quote
  #5  
Old April 13th, 2004, 11:46 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old April 13th, 2004, 12:10 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,195 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 34 m 6 sec
Reputation Power: 252
Quote:
Originally Posted by netytan
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.


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

Reply With Quote
  #7  
Old April 13th, 2004, 12:15 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to create a list of objects


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway