Convert a namedtuple into a dictionary

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 … Read more

Named tuple and default values for optional keyword arguments

I’m trying to convert a longish hollow “data” class into a named tuple. My class currently looks like this: class Node(object): def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right After conversion to namedtuple it looks like: from collections import namedtuple Node = namedtuple(‘Node’, ‘val left right’) But there is … Read more