As part of a program I'm making, I have a class, and have 5 different instances of this class declared globally for accessing by anywhere else in the program. The class has a list associated with it, that should be different for each of the 5 instances of the classes.

The Problem:
I have a function within the class that I use to add a line to the list, when I call this, it doesn't just add it to the list in the first instance of the class, it also adds it to the second instance for some strange reason (but it doesn't add it to any of the other 3 instances of this class). Here is the code below:

# For storing information about auctions
class AuctionList:

    def __init__(self):
        self.auctionData = []   # Unique to each instance

    # Add an auction to the list
    def Add_Auction(self,temp_auction):
        print self
        print "add before"
        print "currentAuctionsListGLOBAL.Get_Num_Auctions():",currentAuctionsListGLOBAL.Get_Num_Auctions()
        print "previousCurrentAuctionListGLOBAL.Get_Num_Auctions():",previousCurrentAuctionListGLOBAL.Get_Num_Auctions()
        print "finishedAuctionListGLOBAL.Get_Num_Auctions():",finishedAuctionListGLOBAL.Get_Num_Auctions()
       
        self.auctionData.append(temp_auction)

        print "add after"
        print "currentAuctionsListGLOBAL.Get_Num_Auctions():",currentAuctionsListGLOBAL.Get_Num_Auctions()
        print "previousCurrentAuctionListGLOBAL.Get_Num_Auctions():",previousCurrentAuctionListGLOBAL.Get_Num_Auctions()
        print "finishedAuctionListGLOBAL.Get_Num_Auctions():",finishedAuctionListGLOBAL.Get_Num_Auctions()

    def Get_Num_Auctions(self):
        return len(self.auctionData)



# 5 Instances of this glass for global access
currentAuctionsListGLOBAL = AuctionList()
fixedPriceOfferAuctionListGLOBAL = AuctionList()
previousCurrentAuctionListGLOBAL = AuctionList()
finishedAuctionListGLOBAL = AuctionList()
successfulAuctionListGLOBAL = AuctionList()

Above is the class, and where the 5 instances are declared.
Below is how I call the Add function above, to add a line to one of the class instances.

import auction_list

auction_list.currentAuctionsListGLOBAL.Add_Auction("line to add")

This would then printout to the terminal the following:

<auction_list.AuctionList instance at 0x0000000002FE25C8>
add before
currentAuctionsListGLOBAL.Get_Num_Auctions(): 1
previousCurrentAuctionListGLOBAL.Get_Num_Auctions(): 1
finishedAuctionListGLOBAL.Get_Num_Auctions(): 0
add after
currentAuctionsListGLOBAL.Get_Num_Auctions(): 2
previousCurrentAuctionListGLOBAL.Get_Num_Auctions(): 2
finishedAuctionListGLOBAL.Get_Num_Auctions(): 0

I also have a bit in the main function to print out the memory location so I know
that they are accessing the correct instance of the class they are supposed to:

def Main():

    print "curr",currentAuctionsListGLOBAL
    print "prev",previousCurrentAuctionListGLOBAL

This prints out:

curr <auction_list.AuctionList instance at 0x0000000002FE25C8>
prev <auction_list.AuctionList instance at 0x000000000300C5C8>

As you can see, when I try to add a line to the class instance currentAuctionsListGLOBAL, it add it to it and previousCurrentAuctionListGLOBAL. But I only want it to be added to the first one currentAuctionsListGLOBAL. I don't understand how it is able to add it to the other? It is using the correct instance for currentAuctionsListGLOBAL, as you can see in the memory location (is that correct terminology?)


Can anyone see my mistake, its really got me stumped. Thx

I could not quite fathom your code but I did the following:

# For storing information about auctions
class AuctionList:

    def __init__(self,data=[]):
        self.auctionData=data

    # Add an auction to the list
    def Add_Auction(self,info):
        print 'Before',self.auctionData     
        self.auctionData.append(info)
        print 'After',self.auctionData
 
    def __len__(self):
        return len(self.auctionData)

    def __str__(self): return str(self.auctionData)

auction2=AuctionList(['line 1'])
auction_list=AuctionList(['line 1'])
auction_list.Add_Auction("line to add")
print 'Length',len(auction_list)
print 'Auction 2, length',len(auction2)
print auction2

Edited 6 Years Ago by pyTony: added auction2

Found the problem.

I was taking the instance list and copying it over to the other instance in another part of the code. I didn't realise that somehow this much link them together and make them the same in python?

Fixed it by putting it through a while loop and appending the data one line at a time. (instead of going inst1.data = inst2.data)

Truly weird behaviour (at least for a C/C++ programmer)
Thx anyway.

This question has already been answered. Start a new discussion instead.