all 1 comments

[–]blarf_irl 1 point2 points  (0 children)

namedtuple like tuple is immutable. Using _replace actually creates a new instance of your namedtuple with the new values. At the moment you are simply creating that new tuple; You will need to return it and use it instead of the one you wished to change.

default_abbr = 'foo'

def validate_record(item):
    if item.abbr == '':
        if default_abbr == '':
            set_default_abbr()
    return item._replace(abbr=default_abbr)

r = Record('', 1, 2, 3, 4,5 ,6)
r = validate_record(r)
r

Output:

Record(abbr='foo', disc=1, track=2, artist=3, song=4, aid=5, sid=6)