python / python/cpython

Clarify the diffrence between object.__sizeof__() and sys.getsizeof() in short documentation/help output

Open
#146,086 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation
Problem

The short documentation shown by help()/pydoc makes object.__sizeof__() and sys.getsizeof() look almost equivalent, even though they may return different values in practice.

For example, their short descriptions currently read very similarly:

  • sys.getsizeof():

    Return the size of object in bytes.

  • object.__sizeof__():

    "Size of object in memory, in bytes."

From these short descriptions, a user can reasonably assume that both functions report the same value.

Why this is confusing

In practice, they can differ.

sys.getsizeof() may return a larger value than obj.__sizeof__(), because it is not just a direct alias for __sizeof__(). The full documentation for sys.getsizeof() explains the extra behavior, but that distinction is not obvious from the short help output.

This makes the following kind of session surprising:

import sys

class A:
    pass

obj = A()

print(obj.__sizeof__())
print(sys.getsizeof(obj))

A user who relies on help() or short docstrings may expect the first two values to match, but they may not.

Suggestion

It may be helpful to clarify this distinction in the short documentation, so that it is visible without referring to the full documentation.

In particular, it could clarify that:

  • sys.getsizeof() may include additional overhead beyond the value returned by __sizeof__()
Linked PRs
  • gh-146087

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the short help or pydoc output for sys.getsizeof() and object.sizeof(), then inspect linked PR gh-146087 for the work already underway. Done means the short descriptions clearly distinguish sys.getsizeof() from sizeof() and explain that additional overhead may be included.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.