Python ones you do not know something - immutable types of caches

First of all, we see the following section of this code :

[python] view plain copy

  1. >>> num1, num2 = 1, 1  
  2. >>> print num1 is num2  
  3. True  
  4. >>> id(num1), id(num2)  
  5. (3965416, 3965416)  
  6. >>> num3, num4 = 1.0, 1.0  
  7. >>> print num3 is num4  
  8. False  
  9. >>> id(num3), id(num4)  
  10. (22416168, 22416184)</span>  


       We can see: num1 and num2 value is 1 , their memory addresses are the same; and num3 and num4 values are also 1.0 , but their memory addresses are different. Why is this?

       In Python , strings and integer objects are immutable ( the immutable ) type, thus python will be very efficient cache them . Such processing can improve the mechanism Python performance. Thus, we see the example below str1 and str2 are also pointing to the same memory address:

[python] view plain copy

  1. >>> str1, str2 = 'a', 'a'  
  2. >>> print str1 is str2  
  3. True  
  4. >>> id(str1), id(str2)  
  5. (22171104, 22171104)</span>  


       Well, is not all integers and strings Python will help us buffer it? The answer is No . As we have time to learn English does not need to remember all the words, but only a select number of high-frequency words. Python also adopt such a strategy. We will see in the example below, not all integers and strings Python will help us Cache:

[python] view plain copy

  1. >>> num5, num6 = 300, 300  
  2. >>> print num5 is num6  
  3. False  
  4. >>> id(num5), id(num6)  
  5. (24972028, 24971416)</span>  

[python] view plain copy

  1. >>> str3, str4 = 'abc', 'abc'  
  2. >>> print str3 is str4  
  3. False  
  4. >>> id(str3), id(str4)  
  5. (29674688, 29674656)</span>  

        Python can help us how much cache it? Python2.3 simple integers buffer range (to 1,100) , Python2.5.4 simple integers range after at least a cache (-5,256) . All short characters are also in the buffer zone.

 

       Since simple integer and single characters can be cached, the same as the immutable type of tuples ( tuple ) can also be cached it? We continue to see the next set of tests :

1.>>> tuple1, tuple2 = (1, ), (1, )  
2.>>> print tuple1 is tuple2  
3.False  
4.>>> id(tuple1), id(tuple2)  
5.(24899856, 24756080)  
6.>>> tuple3, tuple4 = (1, 2), (1, 2)  
7.>>> print tuple3 is tuple4  
8.False  
9.>>> id(tuple3), id(tuple4)  
10.(29705880, 24881752)</span> 

       ( The PS : tuple1 and tuple2 statement when defined as (1) , because the Python symbol () just tuple, which is a packet operator overloading, by the parentheses () first single element package be treated as a grouping operation, instead of tuples)

       We have not seen a tuple showed caching mechanism. Why is that? Because caching mechanism to solve the problem fast data access, simple integer variable and single characters are used more often, but not common tuple tuple say, there is no caching mechanism is also reasonable.

       Of course, in order to better understanding of this phenomenon, we need to learn more and learn Python caching mechanism.

Guess you like

Origin www.cnblogs.com/mana66ccff/p/11130163.html