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.

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.

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]

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.

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. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.