List Append Issue (Python)

Karstein

Senior member
Mar 31, 2011
392
0
71
On the project I'm currently working on, I'm running into an issue with appending results to a list through iteration, in that the entire list is overwritten with the last result found each time.

I'm iterating through a list of filenames. For each iteration, I create a new object instance (SearchResult()) and store the information in it. This is a basic object that holds information about the file (filename, file details etc.)

Finally, I append the object to another list and the next iteration begins.

When iterating through each time, all of the correct information is being gathered and appended. However, each time an item is appended, it overwrites any previous items in the list with that one too. So if you had elements 'A', 'B' and 'C', the list it comes out with is [C, C, C].

Does anyone know why this is? All of the variables in the SearchResult class are self.<var>, so it's not a case of them pointing to the same location there.

Here's a basic overview of the code:

Code:
class SearchResult:
         
    class FilenameInfo:

        def __init__(self):

            self.fileName = ""
    
    class IdTagInfo:
        
        def __init__(self):
            
            self.tagStatus = ""
            self.songArtist = ""
            self.songAlbum = ""
            self.songTrackname = ""
            self.songTracknumber = ""

class DirSpider:

    def __init__(self, dirPath, fileType):
        
        self.dirPath = dirPath
        self.fileType = fileType
        self.songRecord = []
        self.dirList = os.listdir(self.dirPath)

# Directory search function
    def SearchDirectory(self):
        
        # Store list of files found at the specified directory
        
        for fileRecord in self.dirList:
            searchResultInstance = SearchResult()
            searchResultInstance.FilenameInfo.fileName = fileRecord
    
            songData = MP3(fullFile, ID3=EasyID3)
            
            try:
                searchResultInstance.IdTagInfo.songArtist = songData['artist'][0]
                searchResultInstance.IdTagInfo.songAlbum = songData['album'][0]
                searchResultInstance.IdTagInfo.songTrackName = songData['title'][0]
                searchResultInstance.IdTagInfo.songTrackNumber = songData['tracknumber'][0]
            except KeyError:
                print "*** NO DATA ***"
            
            self.songRecord.append(searchResultInstance)
        
        print self.songRecord[0].FilenameInfo.fileName
        print self.songRecord[1].FilenameInfo.fileName
        print self.songRecord[2].FilenameInfo.fileName
        return
Here's some output:

SRI is Maid with the Flaxen Hair.mp3
SRI is Sleep Away.mp3
SRI is test2.mp3
test2.mp3 <--- songRecord[0]
test2.mp3 <--- songRecord[1]
test2.mp3 <--- songRecord[2]

Thanks in advance!
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
You don't actually create instances of your nested classes in SearchResult.

Try something like:
Code:
class SearchResult:
         
    class FilenameInfo:

        def __init__(self):

            self.fileName = ""
    
    class IdTagInfo:
        
        def __init__(self):
            
            self.tagStatus = ""
            self.songArtist = ""
            self.songAlbum = ""
            self.songTrackname = ""
            self.songTracknumber = ""
    def __init__(self):
        self.filename = self.FilenameInfo()
        self.idtag = self.IdTagInfo()

Then change references like:

Code:
searchResultInstance.FilenameInfo.fileName

to

Code:
searchResultInstance.filename.fileName

Your current code is essentially modifying the class objects instead of instance objects.
 

Karstein

Senior member
Mar 31, 2011
392
0
71
Oh wow, I totally missed that I didn't have an init for SearchResult... this is working great now, many thanks OC! :)