Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python Classes Question?

Options
  • 18-12-2007 11:14pm
    #1
    Registered Users Posts: 12,036 ✭✭✭✭


    I have created a class and I want to create an undefined number of objects of this class but, I can't seem to figure out how!

    We'll say this is my class:
    class team:
    [INDENT]def __init__(self, name):
    [INDENT]self.name = name
    self.games = 0[/INDENT][/INDENT]
    

    and I call it in my main program or through another function through:
    teams = []
    for i in range(10):
    [INDENT]name = team("Arsenal")
    teams.append[name][/INDENT]
    

    So basically what I'm looking for is a list of team objects but, they seem to be overwritten and I think I know why (because the object is called the same thing every time it is entered into the list and is so overwritten?)

    I've tried using strings and numbers but I get an error saying:
    can't assign to literal
    

    Anyway, I think I'm rambling...any suggestions?


Comments

  • Closed Accounts Posts: 413 ✭✭sobriquet


    There's an error in your use of "teams.append[name]", that should be "teams.append(name)" - is that just a typo?

    The following works for me:
    >>> class Team:
    ...     def __init__(self, name):
    ...             self.name = name
    ...             self.games = 0
    ... 
    >>> teams = []
    >>> for i in range(10):
    ...     t = Team("My Team %d" % i)
    ...     teams.append(t)
    ... 
    >>> [t.name for t in teams]
    ['My Team 0', 'My Team 1', 'My Team 2', 'My Team 3', 'My Team 4', 'My Team 5', 'My Team 6', 'My Team 7', 'My Team 8', 'My Team 9']
    >>> 
    
    That's in Py 2.5.1 on Ubuntu FYI.


  • Registered Users Posts: 12,036 ✭✭✭✭L'prof


    Yes the teams.append[..] was a typo!

    The team names I'm using are taken from a text file, so I don't really want to mess with them!

    But that code you gave me works so, I think you're onto a winner there in some sense, any other ideas?


  • Closed Accounts Posts: 413 ✭✭sobriquet


    jasonorr wrote: »
    any other ideas?

    Em. About what? You read in lines (or whatever) from your textfile, as you do you create a Team object and add it to the teams list.


  • Registered Users Posts: 12,036 ✭✭✭✭L'prof


    Hey, I've just spotted my errors...not really sure I wanna tell you what it was :rolleyes:

    Firstly, my teams.append(t) line was outside my for loop so only the last team was appended and I also had my teams = [] line inside a while loop instead of just before it so, I think that is where the overwriting occurred :o

    Thanks for your help and sorry for wasting your time :p


  • Closed Accounts Posts: 413 ✭✭sobriquet


    jasonorr wrote: »
    Hey, I've just spotted my errors...not really sure I wanna tell you what it was :rolleyes:

    Firstly, my teams.append(t) line was outside my for loop so only the last team was appended and I also had my teams = [] line inside a while loop instead of just before it so, I think that is where the overwriting occurred :o

    Thanks for your help and sorry for wasting your time :p

    Yeah, those sound like showstoppers alright! No bother.


  • Advertisement
Advertisement