We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,449 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

multiple class instances have same data?

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.

2
Contributors
4
Replies
1 Hour
Discussion Span
3 Years Ago
Last Updated
7
Views
Question
Answered
gorbulas
Newbie Poster
11 posts since Apr 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

IsharaComix
Junior Poster in Training
98 posts since Feb 2010
Reputation Points: 109
Solved Threads: 23
Skill Endorsements: 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]
gorbulas
Newbie Poster
11 posts since Apr 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

IsharaComix
Junior Poster in Training
98 posts since Feb 2010
Reputation Points: 109
Solved Threads: 23
Skill Endorsements: 0
Question Answered as of 3 Years Ago by IsharaComix

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

gorbulas
Newbie Poster
11 posts since Apr 2010
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0978 seconds using 2.7MB