classification
Title: __sizeof__ is not documented
Type: enhancement Stage: needs patch
Components: Documentation Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Philip Dye, aliles, asvetlov, cvrebert, docs@python, eric.snow, jcea, loewis, meador.inge, mjdorma, pitrou, rhettinger, serhiy.storchaka, sptonkin
Priority: normal Keywords: easy

Created on 2012-07-23 19:54 by serhiy.storchaka, last changed 2019-08-09 03:16 by Philip Dye.

Messages (13)
msg166245 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-23 19:54
__sizeof__ is not mentioned on page Doc/reference/datamodel.rst together with other special methods. There is no __sizeof__ in index.
msg166253 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2012-07-23 23:10
Could you possibly write a patch for 2.7, 3.2 and 3.3?
msg166263 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-24 05:45
> Could you possibly write a patch for 2.7, 3.2 and 3.3?

No, not for documentation.
msg166265 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-07-24 06:11
is __sizeof__ part of the language or just an implementation detail?
msg166266 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-24 06:58
See issue2898.
msg166304 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-07-24 15:57
It is part of runtime services and is mentioned in the documentation for 'sys.getsizeof' [1]:

"""
getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.
"""

[1] http://docs.python.org/library/sys.html?highlight=__sizeof__#sys.getsizeof.
msg166309 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-24 16:51
> It is part of runtime services and is mentioned in the documentation for
> 'sys.getsizeof' [1]:

Yes, but it is not mentioned in the index.
msg166353 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-07-25 04:52
issue2898 smells like an implementation detail to me. Also, sys.getsizeof() isn't necessarily authoritative here, since the sys module is full of all sorts of implementation details.

I'm not saying __sizeof__ shouldn't be made an official part of the language, but we should be careful before we effectively etch something in stone for all Python implementations.  It may be worth getting feedback from python-dev, particularly from the major Python implementers.
msg166438 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-07-25 23:03
This may be CPython specific.

Also, sys.getsizeof() hasn't proved to be very useful (I never see anyone use it or see it in production code).  Worse, it doesn't even make sense in the case of shared-key dictionaries and whatnot.  Accordingly, there is little reason to make this more public than it already is.  I would be happy to see it written-off as an implementation detail.
msg166439 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-25 23:05
sys.getsizeof() is not for production code, it's for experimenting and getting information at the command prompt. It's quite useful when you know its limitations.

I'd argue that implementing __sizeof__ for user types doesn't make much sense (except C extension types, which are CPython-specific).
msg166782 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-29 16:18
rhettinger: users frequently need sys.getsizeof. See, for example,

http://stackoverflow.com/questions/1331471/in-memory-size-of-python-stucture
http://stackoverflow.com/questions/449560/how-do-i-determine-the-size-of-an-object-in-python
http://stackoverflow.com/questions/2117255/python-deep-getsizeof-list-with-contents
http://stackoverflow.com/questions/11301295/measure-object-size-accurately-in-python-sys-getsizeof-not-functioning
[and so on]

In what cases, do you think, it generates meaningless results? I find the results for the shared-keys dictionary quite meaningful and natural.

sys.getsizeof is certainly CPython-specific. However, __sizeof__ is not just an implementation detail of sys.getsizeof, just as __len__ is not an implementation detail of len(). Authors of extension types are supposed to implement it if object.__sizeof__ is incorrect.
msg166789 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-07-29 16:57
> sys.getsizeof is certainly CPython-specific. However, __sizeof__ is not
> just an implementation detail of sys.getsizeof, just as __len__ is not an
> implementation detail of len(). Authors of extension types are supposed to
> implement it if object.__sizeof__ is incorrect.

I think there is one difference between __len__ and __sizeof__. __sizeof__ 
should be overloaded only for C-implemented classes. IMHO, it is a part of C 
API.
msg166796 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-29 17:31
> I think there is one difference between __len__ and __sizeof__. __sizeof__ 
> should be overloaded only for C-implemented classes. IMHO, it is a part of C 
> API.

That is a reasonable point. So documenting it along with the type slots
might be best.
History
Date User Action Args
2019-08-09 03:16:37Philip Dyesetnosy: + Philip Dye
2015-03-11 12:02:40serhiy.storchakalinkissue23639 dependencies
2013-01-17 00:38:27asvetlovsetnosy: + asvetlov
2013-01-15 18:27:39serhiy.storchakasetkeywords: + easy
2012-10-30 04:54:19mjdormasetnosy: + mjdorma
2012-10-30 04:53:17sptonkinsetnosy: + sptonkin
2012-10-24 09:43:35serhiy.storchakasetstage: needs patch
type: enhancement
versions: + Python 3.4
2012-08-29 12:04:37alilessetnosy: + aliles
2012-07-29 17:31:57loewissetmessages: + msg166796
2012-07-29 16:57:21serhiy.storchakasetmessages: + msg166789
2012-07-29 16:18:04loewissetnosy: + loewis
messages: + msg166782
2012-07-26 05:59:26cvrebertsetnosy: + cvrebert
2012-07-25 23:05:31pitrousetnosy: + pitrou
messages: + msg166439
2012-07-25 23:03:08rhettingersetnosy: + rhettinger
messages: + msg166438
2012-07-25 04:52:20eric.snowsetmessages: + msg166353
2012-07-24 16:51:44serhiy.storchakasetmessages: + msg166309
2012-07-24 15:57:08meador.ingesetnosy: + meador.inge
messages: + msg166304
2012-07-24 06:58:46serhiy.storchakasetmessages: + msg166266
2012-07-24 06:11:44eric.snowsetnosy: + eric.snow
messages: + msg166265
2012-07-24 05:45:14serhiy.storchakasetmessages: + msg166263
2012-07-23 23:10:00jceasetnosy: + jcea
messages: + msg166253
2012-07-23 19:57:46serhiy.storchakasettitle: __sizeof__ is not documeneted -> __sizeof__ is not documented
2012-07-23 19:54:41serhiy.storchakacreate