Skip to content

recursive for list and tuple seems like a safe default #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cachey/nbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def nbytes(o):
>>> import numpy as np
>>> nbytes(np.ones(1000, dtype='i4'))
4000

>>> nbytes([123]) # doctest: +SKIP
100
"""
name = type(o).__module__ + '.' + type(o).__name__

Expand All @@ -34,5 +37,7 @@ def nbytes(o):
return _array(o)
elif hasattr(o, 'nbytes'):
return o.nbytes
elif isinstance(o, (list, tuple)):
return sys.getsizeof(o) + sum(nbytes(x) for x in o)
else:
return sys.getsizeof(o)