Namedtuples are useful in Python to name small collections of data.
Take for example this namedtuple:
sesameEpisodeNTC = collections.namedtuple('sesameEpisodeNTC',
se0 = sesameEpisodeNTC(lead_character='bigbird', has_elmo=False)
If you make the
namedtuple definition at the module level,
dill can pickle the class with the
namedtuple class object as an attribute. Of course, this doesn't help you "reduce clutter". I think the answer is no. At the moment, you can not define a
namedtuple inside a class and have it pickled by default.
>>> import collections >>> nt = collections.namedtuple('nt',['one','two']) >>> nt <class '__main__.nt'> >>> >>> import dill >>> >>> dill.copy(nt) <class '__main__.nt'> >>> >>> class Foo(object): ... cnt = nt ... >>> f = Foo() >>> f.cnt <class '__main__.nt'> >>> f.cnt(1,2) nt(one=1, two=2) >>> >>> dill.copy(f) <__main__.Foo object at 0x10f1b5850> >>> dill.copy(Foo) <class '__main__.Foo'> >>>
There is a ticket/issue on
dill github to be able to do better, but right now no -- you'd have to live with module-level
namedtuples -- and yes, they need to have the same name as the first parameter of the