1

I gain knowledge about collections module in Python from a few days. During reading about namedtuple I came across a sentence:

Named tuple instances do not have per-instance dictionaries, so they are lightweight and require no more memory than regular tuples.

What do they mean here?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Paulina
  • 53
  • 1
  • 8
  • 4
    Maybe: https://stackoverflow.com/questions/56193305/why-do-namedtuples-use-less-memory-than-dictionaries – j1-lee Apr 17 '22 at 20:32
  • 1
    If you want to post an answer, please do it as an actual answer, not by editing it into the question. You can then also mark your answer as accepted. – mkrieger1 Apr 17 '22 at 21:34

1 Answers1

2

Answer: My explanation based on the link in above comment

Normal objects in python have internal dictionaries to store instances' attributes, since python allows to add custom attributes to class's instances. Due to its internal dictionary, the overhead is bigger. Namedtuple object doesn't allow to add attributes to furher instances, but only to its initial class what is a fair cost saving.

In below code you can see that instance of Foo namedtuple cannot add a new attribute and doesn't have an internal dictionary.

from collections import namedtuple

class Foo:
    pass

f = Foo()
f.a = 1
print(f'f.__dict__:  {f.__dict__}')
print(f'Attribute a: {f.a}')

Output

f.dict: {'a': 1}

Attribute a: 1

Foo = namedtuple("Foo", 'a,b')
f = Foo(1,2)
try:
  print(f'f.__dict__:  {f.__dict__}')
except Exception as e:
  print(e)

Foo = namedtuple("Foo", 'a,b')
f = Foo(1,2)
try:
  f.c = 3
except Exception as e:
  print(e)

Output

Foo object has no attribute dict

'Foo' object has no attribute 'c'

Paulina
  • 53
  • 1
  • 8