[Tutor] question about metaclasses

Steven D'Aprano steve at pearwood.info
Thu Jan 18 22:52:08 EST 2018


On Thu, Jan 18, 2018 at 05:31:24PM +0000, Albert-Jan Roskam wrote:

> > Don't make the mistake of doing this:
> >
> > from collections import namedtuple
> > a = namedtuple('Bag', 'yes no dunno')(yes=1, no=0, dunno=42)
> > b = namedtuple('Bag', 'yes no dunno')(yes='okay', no='no way', dunno='not a clue')
> 
> But if I do:
> Bag = namedtuple('Bag', 'yes no dunno')
> ... and then I create hundreds of Bag instances, this doesn't have a 
> large memory footprint, right? (Because of __slots__) Or is a regular 
> tuple still (much) less wasteful?

Correct.

namedtuple instances are *nearly* as compact as regular tuples. Making 
many instances of the one named tuple class is efficient; making many 
named tuple classes, with one instance each, is slow and wasteful of 
memory.



-- 
Steve


More information about the Tutor mailing list