-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
object.__sizeof__ and sys.getsizeof have the identical docstring description #148046
Copy link
Copy link
Closed as duplicate of#146086
Closed as duplicate of#146086
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Description
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:
- https://stackoverflow.com/questions/55427689/difference-between-sizeof-and-sys-getsizeof
- https://stackoverflow.com/questions/42051378/sizeof-not-getting-called-by-sys-getsizeof
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
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Projects
Status
Todo