Created
November 14, 2019 09:40
-
-
Save krishvishal/2374dcd4307e606f9a50a16293c2aec0 to your computer and use it in GitHub Desktop.
Size of a python object even with recursive encapsulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getsize(obj_0): | |
"""Recursively iterate to sum size of object & members.""" | |
_seen_ids = set() | |
def inner(obj): | |
obj_id = id(obj) | |
if obj_id in _seen_ids: | |
return 0 | |
_seen_ids.add(obj_id) | |
size = sys.getsizeof(obj) | |
if isinstance(obj, zero_depth_bases): | |
pass # bypass remaining control flow and return | |
elif isinstance(obj, (tuple, list, Set, deque)): | |
size += sum(inner(i) for i in obj) | |
elif isinstance(obj, Mapping) or hasattr(obj, iteritems): | |
size += sum(inner(k) + inner(v) for k, v in getattr(obj, iteritems)()) | |
# Check for custom object instances - may subclass above too | |
if hasattr(obj, '__dict__'): | |
size += inner(vars(obj)) | |
if hasattr(obj, '__slots__'): # can have __slots__ with __dict__ | |
size += sum(inner(getattr(obj, s)) for s in obj.__slots__ if hasattr(obj, s)) | |
return size | |
return inner(obj_0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment