#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    9
    Rep 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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo