Skip to content

object.__sizeof__ and sys.getsizeof have the identical docstring description #148046

@peplxx

Description

@peplxx

Documentation

Problem

The docstrings for object.__sizeof__ and sys.getsizeof are practically identical
("Size of object in memory, in bytes." vs "Return the size of object in bytes."),
despite returning different values for the same object.

Neither docstring explains:

  • Why the two values differ
  • When they differ
  • What each one actually measures

This has caused confusions:

Example

>>> x = [1, 2, 3]
>>> print(x.__sizeof__())
72
>>> print(sys.getsizeof(x))
88

Get docstrings

help(object.__sizeof__)

import sys
help(sys.getsizeof)

Help on method_descriptor:

__sizeof__(self, /) unbound builtins.object method
    Size of object in memory, in bytes.

Output of help(object.sizeof)

Help on built-in function getsizeof in module sys:

getsizeof(...)
    getsizeof(object [, default]) -> int

    Return the size of object in bytes.

Output of help(sys.getsizeof)

Proposed fix

object.sizeof:

__sizeof__(self, /) unbound builtins.object method
    Size of object in memory, in bytes, excluding internal
    interpreter overhead. 
    Use sys.getsizeof() to get the total size.

sys.getsizeof:

getsizeof(...)
    getsizeof(object [, default]) -> int

    Return the total size of object in bytes, including
    internal interpreter overhead (such as the garbage
    collector header). Calls the object's __sizeof__
    method and adds the overhead.

Related source code

  • sys.getsizeof implementation code
  • __sizeof__ implementation code

I'd like to submit a pull request for these docstring changes once we agree
on the wording. Feel free to suggest any edits to the proposed text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions