Home>

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)]

basic type

•Boolean

print "size of true:%d"%(sys.getsizeof (true)) print "size of false:%d"%(sys.getsizeof (false))

Output:

size of true:12 size of false:12

•Integer

#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.

•Float

print "size of float:%d"%(sys.getsizeof (1.0))

Output:

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

Output:

1.00000000003 1.00000000001

•String

#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"]])

Output:

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.

Collection type

•List

#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]]])

Output:

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"]))

Output:

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 )

•Tuple

Basically similar to lists,But its minimum occupation is 28 bytes.

•Dictionary

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.

  • Previous js css + html to implement simple calendar
  • Next Problems and solutions in jQuery 30