0

I'm a newcomer to python and I have been exploring the properties of python functions. Suppose I create an simple function foo

def foo():
    pass

Now if I check how much memory is allocated for foo, by doing

import sys
sys.getsizeof(foo) # I get 144 bytes.

Moreover, foo.__dict__ is initially {} and 248 bytes are allocated for foo.__dict__

However, if I add a arbitrary attribute to foo, say foo.foo_attribute="some attribute"then foo.__dict__ gets updated as follows: {foo_attribute:"some attribute"}.

Now if I again check the memory allocation of sys.getsizeof(foo) I still get 144 bytes, even though the function's dict attribute was updated. Moreover, foo.__dict__ still remains 248 bytes.

I was expecting the size of the function foo to increase after adding an arbitrary attribute to foo and also the size of foo.__dict__, however neither of that occured and the memory size stayed the same.

Any reason why this behavior occurs?

Thanks in advance!

Rohan
  • 81
  • 2
  • 8

0 Answers0