def total_size(o, handlers={}, verbose=False):
 def dict_handler(d):
  return chain.from_iterable(d.items())
 all_handlers = {tuple: iter, list: iter, deque: iter, dict: dict_handler, set: iter, frozenset: iter}
 all_handlers.update(handlers)
 seen = set()
 default_size = getsizeof(0)
 def sizeof(o):
  if (id(o) in seen):
   return 0
  seen.add(id(o))
  s = getsizeof(o, default_size)
  for (typ, handler) in all_handlers.items():
   if isinstance(o, typ):
    s += sum(map(sizeof, handler(o)))
    break
  return s
 return sizeof(o)

def total_size ( o , handlers = { } , verbose = False ) : def dict_handler ( d ) : return chain . from_iterable ( d . items ( ) ) all_handlers = { tuple : iter , list : iter , deque : iter , dict : dict_handler , set : iter , frozenset : iter } all_handlers . update ( handlers ) seen = set ( ) default_size = getsizeof ( 0 ) def sizeof ( o ) : if ( id ( o ) in seen ) : return 0 seen . add ( id ( o ) ) s = getsizeof ( o , default_size ) for ( typ , handler ) in all_handlers . items ( ) : if isinstance ( o , typ ) : s += sum ( map ( sizeof , handler ( o ) ) ) break return s return sizeof ( o )
returns the approximate memory footprint an object and all of its contents .