python mechanism with small data pool

python mechanism

  1. id is ==
    • In Python, what id is? id is a memory address, it was asked, what is the memory address it? As long as you create a data (objects) that will open up a space in memory, this temporary increase in data into memory, so this space is to have a unique identity, like identity card number, identifying this space is called a memory address, that is, the data (object) id, then you can use the id () to get the data memory address:
      name = 'Bai'
      Print (the above mentioned id (name)) # 1,585,831,283,968

    • So what is that? == What it is?
      == is the numerical comparison of both sides are equal, and is comparable on both sides of a memory address are equal. If memory addresses are equal, then this is actually on both sides point to the same memory address.
    • If you can say the same memory address, then certainly the same value, but if the values ​​are the same, not necessarily the same memory address
  2. Block
    • Code block: all the code we need to rely on the code block execution
    • A file is a block of code (except function)
    • The next line interactive command is a code block
  3. Two mechanisms: the same next block of code, there is a mechanism different code blocks, follow another mechanism.
  4. Mechanism same cache block
    • Prerequisites: the same block of code.
    • Mechanism content: Python commands executed during the same block of code to initialize the object, checks whether the value already exists, if present, it will be reused. In other words: When you perform the same block, encountered command to initialize the object, he will be initialized with the value of this variable is stored in a dictionary, in the face of new variables will first query records in the dictionary, If you have the same record then it will re-use this value before this dictionary. So you example given, when the file executed (same block) will i1, i2 two variables point to the same object, then they meet the caching mechanism in the presence of only one memory, namely: the same id.
    • Suitable for: int str bool
    • Specific rules: All numbers, almost all of the strings
    • Advantages: improve performance, save memory.
  5. Caching mechanism under different code blocks: small data pool
    • Prerequisites: the different code blocks.
    • Mechanism content: Python automatically integer from -5 to 256 were cached, when you these integer assigned to the variable, and will not re-create the object, but the use of already created the cache object.
      python string will be certain rules of the pool resides in a string to create a copy when you these strings assigned to the variable, and will not re-create the object, but the use of the pool is created that resides in a string Object.
        In fact, both the cache or pool resides strings are made of a python optimization, is to string integer, and certain rules of ~ 5-256, put in a 'pool' (container, or dictionaries), whether the program variables that point to a string or integer within these ranges, then he quoted directly in the 'pool', the implication is to create a memory of.
    • Suitable for: int str bool
    • Specific rules:
      • int: So we all know that for integers, the range of small data pool is -5 to 256, if multiple variables are pointing to the same (within this range) numbers, they point to in memory is a memory address
      • str: String from following the general direction of these discussions (you can understand!):
        1. String length of 0 or 1, the default mechanism are employed resident (pool small data).
        2. Length of the string> 1, and contains only lowercase letters, numbers, underline, the default will reside.
        3. Multiplication string obtained under two conditions.
          • The multiplier is 1
            • Contains only uppercase and lowercase letters, numbers, underscores, the default resides
            • Containing other characters, the length of <= 1, default residing
            • Containing other characters, the length of> 1, default resides.
          • Multiplier> = 2:
            • Containing only lowercase letters, numbers, underscores, the total length of <= 20, default dwell
        4. Specify the Park.
    • Advantages: improve performance, save memory.

Guess you like

Origin www.cnblogs.com/940531gbh/p/11262667.html