Created
July 21, 2016 05:55
-
-
Save mylifeisshan/7129b98358e2a052fe7bad3e1d6eb29d to your computer and use it in GitHub Desktop.
Pysize Snippet
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
import sys | |
def get_size(obj): | |
"""Recursively finds size of objects""" | |
size = 0 | |
if isinstance(obj, dict): | |
size += sum([get_size(v) for v in obj.values()]) | |
size += sum([get_size(k) for k in obj.keys()]) | |
elif hasattr(obj, '__dict__'): | |
size += get_size(obj.__dict__) | |
elif hasattr(obj, '__iter__'): | |
size += sum([get_size(i) for i in obj]) | |
else: | |
size += sys.getsizeof(obj) | |
return size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment