This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: OrderedDict doesn't implement __sizeof__
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: belopolsky, pitrou, rhettinger
Priority: low Keywords: patch

Created on 2010-09-15 21:12 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
od_size.diff rhettinger, 2010-09-16 00:53 Patch for a __sizeof__ method.
Messages (8)
msg116481 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-15 21:12
Ordered dicts pretend to have the memory consumption as dicts:

>>> import sys, collections
>>> sys.getsizeof({})
280
>>> sys.getsizeof(collections.OrderedDict())
280
msg116484 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-09-15 21:28
I wonder if type metaclass can be taught to handle this in general for subclasses implemented in python.
msg116485 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-15 21:30
How do you want to "handle this in general"?
msg116487 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-09-15 21:37
> How do you want to "handle this in general"?

Possibly by adding up __sizeof__'s of base and __dict__ in a generated function?  With a special handling of slots ...

May prove to be too complicated to be worth the effort.
msg116488 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-15 21:39
> > How do you want to "handle this in general"?
> 
> Possibly by adding up __sizeof__'s of base and __dict__ in a generated
> function?  With a special handling of slots ...

I agree that in the general case this would be better, but for
OrderedDict it would still fail to give the right value (OrderedDict
also needs to take into account its internal node objects).
msg116492 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-15 21:52
I have a hard time caring about this.  The main value of sys.getsizeof()
is to enable developers to determine the internal sizes of basic objects on a given build.  IIRC, there was no intention for this to become a requirement for pure python classes.
msg116535 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-09-16 12:28
The patch looks good.
msg116993 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-20 22:32
See r84852
History
Date User Action Args
2022-04-11 14:57:06adminsetgithub: 54074
2010-09-20 22:32:53rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg116993
2010-09-16 12:28:08pitrousetmessages: + msg116535
2010-09-16 00:53:31rhettingersetfiles: + od_size.diff
keywords: + patch
type: behavior -> enhancement
stage: patch review
2010-09-15 21:52:48rhettingersetassignee: rhettinger
messages: + msg116492
2010-09-15 21:39:48pitrousetmessages: + msg116488
2010-09-15 21:37:16belopolskysetmessages: + msg116487
2010-09-15 21:30:37pitrousetmessages: + msg116485
2010-09-15 21:28:58belopolskysetnosy: + belopolsky
messages: + msg116484
2010-09-15 21:12:37pitroucreate