Python small Data pool, code block

Source: Internet
Author: User

One, code block

Python programs are constructed from blocks of code. A block is the text of a Python program that executes as a unit.

Code blocks: A module, a function, a class, a file, and so on are all blocks of code.

Each command entered as an interactive method is a block of code.

What do you mean by interactive mode? Is that we enter the Python interpreter in CMD, and each line of code is a block of code.

Different blocks of code:

def func ():    Print (333) class A:    name = ' Alex '

Although the indentation above is called a block of code, he is not a tightly defined block of code in Python.
For i in ' 12324545 ':
Print (i)


In interactive mode, each line is a block of code
>>>i1 = to understand that this line is in a file
>>>i2 = to understand that this line is in another file

  

# Second, id, is = =
name = ' Alex ' #赋值
Print (' alex ' = = ' Alex ') #数值相同
name = ' alex123 '
Print (name) #2112772900544

# in-memory IDs are unique, and if the two variables point to the same ID of the value, it proves that they are the same in memory.

# is determines whether the ID value of two variables is the same.
# If IS is true, = = must be true.

# Three, small data pool (caching mechanism, resident mechanism)

Application data types for small data pools: integers, strings, bool values


# Small Data pool, python to make an optimization of memory:
# He will -5~256 a string between integers, as well as certain regular strings, to advance in memory to create pools, containers. These numbers are fixed in the container.
Point to the same memory address
In the interactive device
S1 = 100
S2 = 100
Point to address consistent
And
S1 = 1000
S2 = 1000
Point to address inconsistency

Why do you do this:
1. Save memory.
2. Improve performance and efficiency

Code block:
Python puts the corresponding relationship of variables and values into a dictionary when initializing an object's command for a variable in the same code block.
If the following code encounters the command that initializes the object, it looks for it from the dictionary, and if the same value exists, he will reuse it, so it points to the same memory address.
DIC = {' name ': [email protected] memory address}

Python interacts with different blocks of code: When initializing a command, he will look for it from the small data pool.
name = ' [email protected] '
name1 = ' [email protected] '

1 #The memory address will change2 3 #small Data pool points to the same address4I1 = 1005i2 = 1006 Print(ID (i1), ID (i2))7 #pycharm 140708817920128 1407088179201288 #Interactive mode 140708990083200 1407089900832009 Ten  One #The same code block. Pycharm points to the same address, the different addresses that the interaction is pointing to.  AI1 = 1000 -i2 = 1000 - Print(ID (i1), ID (i2)) the #pycharm 2093897078224 2093897078224 - #Interactive mode 1830950750224 1830950750096 -  -  + #different code blocks, different addresses - deffunc1 (): +i = 1000 A     Print(ID (i))#2093927219472 at  - defFunc2 (): -i = 1000 -     Print(ID (i))#2093927219440 -  - func1 () in Func2 () -  to  + #get the same address from small data - deffunc1 (): thei = 100 *     Print(ID (i))#140708817920128 $ Panax Notoginseng defFunc2 (): -i = 100 the     Print(ID (i))#140708817920128 +  A func1 () theFunc2 ()
Small Data pool code block detailed

Python small Data pool, code block

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.