0

It looks like things are going wrong on line 9 for me. Here I wish to push a new copy of the TagsTable into a dictionary. I'm aware that once a namedtuple field is recorded, it can not be changed. However, results baffle me as it looks like the values do change - when this code exits all entries of mp3_tags[ any of the three dictionary keys ].date are set to the last date of "1999_03_21"

So, two questions:

  1. Is there a way to get a new TagsTable pushed into the dictionary ?

  2. Why doesnt the code fail and not allow the second (and even third) date to be written to the TagsTable.date field (since it seems to be references to the same namedtuple) ? I thought you could not write a second value ?

from collections import namedtuple
2   TagsTable = namedtuple('TagsTable',['title','date','subtitle','artist','summary','length','duration','pub_date'])
3   mp3files = ['42-001.mp3','42-002.mp3','42-003.mp3']
4   dates = ['1999_01_07', '1999_02_14', '1999_03_21']
5   
6   mp3_tags = {}
7   
8   for mp3file in mp3files:
9       mp3_tags[mp3file] = TagsTable
10  
11  for mp3file,date_string in zip(mp3files,dates):
12      mp3_tags[mp3file].date = date_string
13      
14  for mp3file in mp3files:
15      print( mp3_tags[mp3file].date )
RichWalt
  • 502
  • 1
  • 4
  • 13

1 Answers1

0

looks like this is the fix I was looking for:

from collections import namedtuple

mp3files = ['42-001.mp3','42-002.mp3','42-003.mp3']
dates = ['1999_01_07', '1999_02_14', '1999_03_21']

mp3_tags = {}

for mp3file in mp3files:      
   mp3_tags[mp3file] = namedtuple('TagsTable',['title','date','subtitle','artist','summary','length','duration','pub_date'])

for mp3file,date_string in zip(mp3files,dates):
      mp3_tags[mp3file].date = date_string

for mp3file in mp3files:
      print( mp3_tags[mp3file].date )
RichWalt
  • 502
  • 1
  • 4
  • 13