On Python 3.5.0:
>>> from collections import namedtuple
>>> cluster = namedtuple('Cluster', ['a', 'b'])
>>> c = cluster(a=4, b=9)
>>> c
Cluster(a=4, b=9)
>>> vars(c)
OrderedDict([('a', 4), ('b', 9)])
On Python 3.5.1:
>>> from collections import namedtuple
>>> cluster = namedtuple('Cluster', ['a', 'b'])
>>> c = cluster(a=4, b=9)
>>> c
Cluster(a=4, b=9)
>>> vars(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vars() argument must have __dict__ attribute
Seems like something about namedtuple
changed (or maybe it was something about vars()
?).
Was this intentional? Are we not supposed to use this pattern for converting named tuples into dictionaries anymore?
Per Python bug #24931:
Revision that made the change
Specifically, subclasses without
__slots__
defined would behave weirdly:Use
._asdict()
.From the docs
The docs (and
help(namedtuple)
) say to usec._asdict()
to convert to a dict.__dict__
was implemented as a@property
and has been removed; you can see the change in the source code:3.5.0:
3.5.1: