Everything is an object
Everything is an object in python,Including all types of constants and variables,Integers, Booleans, and even functions. See a question on stackoverflow is everything an object in python like ruby
You can verify in the code:
#everythin in python is object def fuction ():return print isinstance (true, object) print isinstance (0, object) print isinstance ("a", object) print isinstance (fuction, object)
How to calculate
Python provides the function getsizeof in the sys module to calculate the size of python objects.
sys.getsizeof (object [, default]) Returns the size of the object in bytes. This object can be any type of object. So the built-in objects can return the correct results, but they are not guaranteed to be valid for third-party extensions. Because it is related to the specific implementation. ... getsizeof () calls the __sizeof__ method of the object. If the object is managed by the garbage collector, This adds extra garbage collector overhead.
Of course, the object memory footprint is closely related to the Python version and the operating system version. The code and test results of this article are based on windows7 32-bit operating system.
import sys print sys.version
2.7.2 (default, jun 24 2011, 12:21:10) [msc v.1500 32 bit (intel)]
print "size of true:%d"%(sys.getsizeof (true)) print "size of false:%d"%(sys.getsizeof (false))
size of true:12 size of false:12
#normal integer print "size of integer:%d"%(sys.getsizeof (1)) #long print "size of long integer:%d"%(sys.getsizeof (1l)) print "size of big long integer:%d "%(sys.getsizeof (100000l)) output:
size of integer:12x size of long integer 1l:14 size of long integer 100000l:16
It can be seen that the integer type occupies 12 bytes, the long integer type occupies at least 14 bytes, and the occupied space will increase as the number of bits increases. In the 2.x version, if the value of the integer type exceeds sys.maxint, it will automatically expand to a long integer.After Python 3.0, integers and long integers were unified into one type.
print "size of float:%d"%(sys.getsizeof (1.0))
size of float:16
The floating point type occupies 16 bytes. It will be rounded up if it exceeds a certain precision.
Consider the following code:
print 1.00000000003 print 1.000000000005
#size of string type print "\ r \ n" .join (["size of string with%d chars:%d"%(len (elem), sys.getsizeof (elem)) for elem in ["", " a "," ab "]]) #size of unicode string print" \ r \ n ".join ([" size of unicode string with%d chars:%d "%(len (elem), sys.getsizeof (elem )) for elem in [u "", u "a", u "ab"]])
size of string with 0 chars:21 size of string with 1 chars:22 size of string with 2 chars:23 size of unicode string with 0 chars:26 size of unicode string with 1 chars:28 size of unicode string with 2 chars:30
The ordinary empty string occupies 21 bytes, each additional character,Occupies an extra byte. A unicode string occupies a minimum of 26 bytes, each additional character,Takes up 2 more bytes.
#size of list type print "\ r \ n" .join (["size of list with%d elements:%d"%(len (elem), sys.getsizeof (elem)) for elem in [, [ 0], [0,2], [0,1,2]]])
size of list with 0 elements:36 size of list with 1 elements:40 size of list with 2 elements:44 size of list with 3 elements:48
The visible list occupies a minimum of 36 bytes, with each additional element,Add 4 bytes. But beware,The sys.getsizeof function does not calculate the element size of the container type.such as:
print "size of list with 3 integers%d"%(sys.getsizeof ([0,1,2])) print "size of list with 3 strings%d"%(sys.getsizeof (["0", "1 ","2"]))
size of list with 3 integers 48 size of list with 3 strings 48
The container should hold a reference to the element.If i want to calculate the container accurately,Can refer torecursive sizeof recipe. Use its total_size function:
print "total size of list with 3 integers%d"%(total_size ([0,1,2])) print "total size of list with 3 strings%d"%(total_size (["0", "1", "2"]))
The output is:
total size of list with 3 integers 84 total size of list with 3 strings 114
It can be seen that the space occupied by the list is the basic space 36 + (object reference 4 + object size) * number of elements.
Also note that if you declare a list variable,It will allocate some space in advance,To increase efficiency when adding elements:
li= for i in range (0, 101):print "list with%d integers size:%d, total_size:%d"%(i, getsizeof (li), total_size (li)) li.append (i )
Basically similar to lists,But its minimum occupation is 28 bytes.
The situation of the dictionary is relatively complicated,Of courseReference code dictobject.cIn additionnotes on optimizing dictionariesIt is well worth reading carefully.
The basic situation can refer to the [stackoverflow] questionpython "s underlying hash data structure for dictionariesSome answers in:
• The dictionary has a minimum of 8 entries (pydict_minsize);•When the number of entries is less than 50,000, each time increases by 4 times;•When the number of entries is greater than 50,000, each time doubles;The hash value of the key is cached in the dictionary,The dictionary is not recalculated after resizing;
The dictionary is resized every 2/3.
- Detailed use of __new __ () method in Python
- Detailed __new__, __init__, __call__ three special methods in Python
- __New__ special method in Python
- Detailed differences between __new__ and __init__ methods in Python
- In-depth understanding of __new__ and __init__ and their differences in Python
- __New__ and __init__ magic methods in Python
- Example of calculating the memory size of an object in Python
- Learn Python in depth with variables and reference objects
- Python uses __new __ () method to allocate memory for an object and returns a reference to the object
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- dart - flutter: the instance member'stars' can't be accessed in an initializer error