I use the builtin function vars
to get the namedtuple as a dictionary.
However, it returns an OrderedDict
which pprint
won't indent, so I convert it to a dict
:
>>> from collections import namedtuple
>>> Busbar = namedtuple('Busbar', 'id name voltage')
>>> busbar = Busbar(id=102, name='FACTORY', voltage=21.8)
With pprint
and dict
:
>>> from pprint import pprint
>>> pprint(dict(vars(busbar)))
{'id': 102,
'name': 'FACTORY',
'voltage': 21.8}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…