1,105,585 Community Members

multiple class instances have same data?

Member Avatar
gorbulas
Newbie Poster
11 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a class, and create 4 instances of this class as per below:

class AuctionList:
def __init__(self):
        self.Clear_Auction_List()

# Data
auctionList = []

#Functions
    ....
currentAuctionList = AuctionList()
previousCurrentAuctionList = AuctionList()
finishedAuctionList = AuctionList()
successfulAuctionList = AuctionList()

The class has a list variable and a few functions to manipulate it.

When I add something to the first list, for some reason it also adds it to all the other instances also.

currentAuctionList.AddToList("Tree")

I then call a method in the class that returns the length of the list for each instance of the class, and they all say they are length of 1, even though I only added it to the first class.

finished auctionlist length:
1
successful auctionlist length:
1
current auctionlist length:
1
previous current auctionlist length:
1

I expect this is some kind of newbie trap, or something.

Member Avatar
IsharaComix
Junior Poster in Training
98 posts since Feb 2010
Reputation Points: 97 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

You aren't showing your complete class. It looks like you may have accidentally made something global that should have been local to your auction list.

Here's how I would create a class based on your specs. Does your code look something like this?

class AuctionList(object):
    def __init__(self):
        self.mylist = []
    def AddToList(self, obj)
        self.mylist.append(obj)

The way all of your code is aligned in your sample implies that your data is not indented to match the class it belongs to. I'm sure you know this already, but indentation is significant and required in Python. Take another look at it and see if that's what's messing up your code.

Member Avatar
gorbulas
Newbie Poster
11 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks, indentation is all fine, and yea that's exactly what my functions look like, here's the class below:

class AuctionList:

    def __init__(self):
        self.Clear_Auction_List()

    # Data
    auctionData = []                    # Auction number, Auction title, Item ID, Reserve, Buy Now, Start Price, Current Bid, Bids, Watchers, Views, Unanswered Questions, Auction Length, Auction Finish Time
    auctionList = []         # List of auctionData instances


    # Clear the auction list
    def Clear_Auction_List(self):
        while len(self.auctionList) > 0:
            self.auctionList.pop()

    # Load the auction list
    def Load_Auction_List(self,filename):
        interface = CSVInterface()
        interface.Load_Auction_List(self,filename)

    # Save the auction list
    def Save_Auction_List(self,filename):
        interface = CSVInterface()
        interface.Save_Auction_List(self,filename)

    # Add an auction to the list
    def Add_Auction(self,temp_auction,credibility):
        if credibility == 1:
            self.auctionList.append(temp_auction)
        

    # Get the number of auctions in the list
    def Get_Num_Auctions(self):
        return len(self.auctionList)

    # Get an auction
    def Get_Auction(self,auct_num):
        return self.auctionList[auct_num]
Member Avatar
IsharaComix
Junior Poster in Training
98 posts since Feb 2010
Reputation Points: 97 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
1
 

Ah... OK. Do you see how you declare AuctionData and AuctionList in lines 7 and 8? Those are not aligned with the init function and are not referenced using 'self', so instead of becoming part of an instance of an AuctionList, they are just like static variables belonging to the entire AuctionList class (this is probably very different from other languages you've probably used).

Define your __init__ like this:

def __init__(self):
        self.auctionData = []
        self.auctionList = []
        self.Clear_Auction_List()

This sets them as local variables, and it should behave properly.

Question Answered as of 4 Years Ago by IsharaComix
Member Avatar
gorbulas
Newbie Poster
11 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes thanks, that solved it.

I agree its a little different, I usually work in C or C++ and you don't have to specifically set the variables within a class non-static. Thanks again. :-)

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: