I have a named tuple class in python
class Town(collections.namedtuple('Town', [
'name',
'population',
'coordinates',
'population',
'capital',
'state_bird'])):
# ...
I’d like to convert Town instances into dictionaries. I don’t want it to be rigidly tied to the names or number of the fields in a Town.
Is there a way to write it such that I could add more fields, or pass an entirely different named tuple in and get a dictionary.
I can not alter the original class definition as its in someone else’s code. So I need to take an instance of a Town and convert it to a dictionary.