Python code block small Data pool

Source: Internet
Author: User

A block of code

What is a code block???

Python programs are constructed from blocks of code. Let me give you an example:

1  for inch ' 12334567 ' : 2     Print (i)
1 def func (): 2     Print ('hello,world! ')
1 class A: 2     ' Colin '

The above example can be called a block of code, although the contents of the indentation above are called blocks of code, but they are not strictly defined chunks of code in Python.

So how do you define the code blocks in Python that really matter?

So we define: a module, a function, a class, a file, and so on, is a block of code.

In addition, in computer terminal mode, we can also consider a block of code for each command entered. Such as:

The relationship between the two IDs, is, = = what is the ID in python?

We can understand that the ID is the memory address of the computer, when you use the ID () built-in function to query the memory address of a data, for example:

1 ' Colin ' 2 Print (ID (name))    #

From the above code analysis, when the ID () instruction in Python is called, the memory address of the variable name is output, its address position is: 1986166776248, after the second call, the memory address is changed, is: 1957356364216. It is known that the variable in memory address is not fixed.

What is? What is the = =?

We use is to determine if the ID value of the two variables is equal, and ' = = ' is the value that indicates whether the values on either side of the symbol are equal.

If the memory addresses are equal, then = = both points to the same memory address.

In general, we summarize: if is true, then = = must be True

Three small data pools (caching mechanism, resident mechanism)

What is a small data pool?

Small data pools, also known as small integer caching mechanisms, or host mechanisms, are primarily used for Python's memory optimizations, typically 5! ~256 integers, as well as certain regular strings, are created in memory in advance of pools, containers, and containers fixed into these numbers.

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

And why use small data pools???

The main advantages of the following two points:

1. Save Memory,

2. Improve performance and efficiency.

Python creates a copy of a regular string in a string-resident pool , and when you assign those strings to a variable, it does not recreate the object, but instead uses the object that was created in the string-resident pool.

int: for integers, the range of small data pools is -5~256, and if multiple variables are numbers that point to the same (within that range), they point to a memory address in memory.

str: The string should be discussed from the following general directions:

1, the length of the string is 0 or 1, the default is to use a resident mechanism (small data pool)

2, the length of the string is >1, and it only contains uppercase and lowercase letters, numbers, and underscores, it will only be resident by default.

3, the string obtained by multiplication, in two cases.

3.1 Multiplier is 1 o'clock:

Contains only uppercase and lowercase letters, numbers, underscores, and default residency.

Contains other characters, length <=1, default resident.

Contains other characters, length >1, default resident.

  3.2 Multiplier when >=2:

Contains only uppercase and lowercase letters, numbers, underscores, total length <=20, default resides.

In the python3 of a large environment:

STR: Memory (internal) encoding is Unicode

Bytes:python is one of the underlying data types, and STR is equivalent to the twins, all methods that Str has, and the bytes type applies.

Encodings do not recognize each other:

1. Due to the inability to identify each other between encodings. (because: the STR internal encoding is in Unicode mode.) )

2. Network transmission, or 01010101 of the hard disk storage, must be a non-Unicode encoding of 01010101.

# str--------> Bites encode encoding

1S1 ='Colin'2S2 ='Tomorrow'3B1 = S1.encode ('Utf-8')4 Print(B1)#b ' Colin '5B2 = S2.encode ('GBK')6 Print(B2)#b ' \xc3\xf7\xcc\xec '

# bytes--------> str decode decoding

1 b1 = b'\xc3\xf7\xcc\xec'     #  GBK's bytes2 s2 = B1.decode ('gbk')3print(s2)    # Tomorrow

The addresses that are heavily referenced in this article are:

http://www.cnblogs.com/jin-xin/articles/9439483.html#4039042

Python code block small Data pool

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.