Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Python Classes Question?

  • 18-12-2007 10:14PM
    #1
    Registered Users, Registered Users 2 Posts: 12,053 ✭✭✭✭


    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, Registered Users 2 Posts: 12,053 ✭✭✭✭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, Registered Users 2 Posts: 12,053 ✭✭✭✭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