3

I have a dict, its size is 198526.

sys.getsizeof(dic))  # 198526

class Actor(object):
    def __init__(self):
       pass

actor = Actor()
actor.__dict__.update(dic)

sys.getsizeof(actor.__dict__)  #89850

Why actor.__dict__ < dict ?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
huang
  • 521
  • 3
  • 11

1 Answers1

0

Here's a possible explanation: Python dictionaries never shrink.

If that dictionary had more elements but then you removed some of them, the hash table won't be resized.

OTOH if you copy it or update another dictionary, then you might get a smaller (hash table) size.

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • That may be so, but simply trying his example does make me wonder why a standalone empty dict is always larger than the class state. For example: `sys.getsizeof({}) = 288` while `sys.getsizeof(type('', (object,), {}).__dict__) = 48`. Either `sys.getsizeof()` is wrong or it's a reference to a different object in memory. – Wolph Oct 26 '15 at 11:10
  • 1
    `getsizeof` is not wrong. `type('', (object,), {}).__dict__` -> `` – Karoly Horvath Oct 26 '15 at 11:12
  • In that case, that should be the answer to the question. While your explanation isn't wrong, it doesn't apply here. – Wolph Oct 26 '15 at 21:52
  • @Wolph: http://stackoverflow.com/questions/25440694/whats-the-purpose-of-dictproxy `dictproxy` objects are the `__dict__` of *types*. So it doesn't apply here. – Karoly Horvath Oct 26 '15 at 22:35
  • that may be true, but in the question he appears to be comparing the dictproxy to the dict as well so the actual size of the dict might not be measured. – Wolph Oct 26 '15 at 23:25
  • @Wolph: I don't see anything in the question that suggests that. Can I ask you to stop spamming this answer with irrelevant comments? SO has this wonderful new feature where you can post questions. – Karoly Horvath Oct 26 '15 at 23:30
  • Either you are misunderstanding my comments or you are unclear on the definition of a question (hint: they usually involve question marks). Let me try to clarify once more. The question was why some dictionary `dic` is smaller than the `__dict__` of the `Actor` object. This could be due to resizing but as we discussed earlier, it is most likely due to not being a full dict. – Wolph Oct 28 '15 at 01:22
  • I'm not misunderstanding your comments. It could be even another type of dict (though not likely). I'm just not in the mood (well, never in the mood) to listen to this s*t. "In that case, that should be the answer to the question. While your explanation isn't wrong, it doesn't apply here." "he appears to be comparing the dictproxy" "you are misunderstanding my comments" EOF. – Karoly Horvath Oct 28 '15 at 09:03