You can use the sys.getsizeof() function from the sys module.
The sys.getsizeof(object[,default]) function returns the size of the object in bytes.The objects can be of any type and this function is implementation specific which means it will return correct results for built-in objects but for third-party extensions it may not be true.
Here, the default argument allows you to define a value which will be returned if the object does not provide means to retrieve the size , else a TypeEror may be raised.
The getsizeof() calls the _sizeof_ method of the object and adds extra garbage collector overhead in case, the garbage collector manages the object.
Ex-
>>> import sys
>>> a = 2
>>> sys.getsizeof(a)
24
>>> sys.getsizeof(sys.getsizeof)
32
>>> sys.getsizeof('hello')
38
>>> sys.getsizeof('hello world')
48
Note:
getsizeof() function was introduced in Python 2.6 version.
If you are using an older version (lower than Python 2.6) then refer to this doc http://code.activestate.com/recipes/546530/ .
Hope this answer helps!