ordereddict 或 dict 的大小小于组成元素?

Size of ordereddict or dict less than the constituent elements?

我有一个 class 匹配模式。

class EnvelopeData(object):

  def __init__(self):
    self.envelope_data = OrderedDict()
    self.table = table_name
    self.payload = payload

  def get_envelope_data(self, use_hex_encoding):
    """
      Getting the envelope data to be sent with the actual record payload

      :param use_hex_encoding:
      :raise:
    """
    self.envelope_data["table"] = self.table.encode(STRING_ENCODING)
    self.envelope_data["payload"] = Utility.get_serialized_avro(self.table, 
                                                             hex_encoding=use_hex_encoding)

    print "For schema_name ", self.schema, sys.getsizeof(self.envelope_data["table"])+sys.getsizeof(self.envelope_data["payload"])

还有当我打印时

a = EnvelopeData()
a.get_envelope_data()
print sys.getsizeof(a.envelope_data)

内部元素的大小 > 包含该元素的 ordereddict 的大小。因此,我对字典如何占用比其构成元素更少的空间感到困惑。

通过sys.getsizeof()文档你可以看到它具体说:

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

意思是,字典中的项目不计入大小的一部分,只计入字典对象本身。
它还链接到递归 sizeof recipe,它将包含字典中的项目。