DAY6 python code block, small data pool detailed

Source: Internet
Author: User

I. Today's content

 (a) Is,==,id

(ii) meaning of the code block

(iii) Small data pool

(iv) Python code II

 

(a) code block

A python program is made up of blocks of code that are text of a Python program and are executed as a unit.

  Code block: A module, a function, a file is a block of code. However, in an interactive environment (the terminal environment), each input command, each line is a block of code.

And the two functions in a file are different blocks of code, respectively:

(ii) ==,id (), is

' = = ': Used to determine whether the contents of two objects are equal

#一个 "=" means an assignment expression name = ' he ' # ' = = ' means the same print for the value content (' he ' = = ' he ') >>> truename = ' he ' name1 = ' Alex ' Print (name = = Name1 ) >>>false

ID (): Returns the memory address of the object in memory

1. In memory, the ID is unique

2. If the values of the two variables point to the same ID, it proves that they are the same in memory.

2101255303664print (ID (name1)) >>> 2101255303664

 is: Determine if two values are the same as the ID (memory address)

name = ' he ' name1 = ' he ' Print (name1 is name) >>> True

Conclusion: If is true, the memory address is the same, the value must be the same, if the value is the same, the memory address may not be the same.

 

(iii) Small data pool (caching mechanism, resident mechanism)

Small Data pool: Also known as small integer caching mechanism, resident mechanism.

  Small Data Pool premise: Small Data pool data type only for "integer, string, bool value"

Concept:    Small Data pools are Python's optimizations for memory, -5~256 integers, and strings of certain rules, created in memory in advance
The pool, where the data is fixed in the pool. Advantages: 1. Save Memory
2. Improve performance and efficiency

  int: An integer of -5~256 that, if multiple variables point to integers in this range at the same time, they will point to the same memory address in memory.

 STR: What is a certain string of rules??

(1) When the length of the string is 0 or 1 o'clock, the small data pool residency mechanism is used by default.

  

(2) When the length of the string is greater than 1 o'clock, if it is made up of only English letters, numbers and underscores, the default is to use a small data pool residency mechanism.

  

 (3) The string obtained by "*" can be divided into two situations:

3.1 Multiplier is 1, default resident

  

    3.2 Multiplier is 2, but the total string length is less than 20 o'clock, the default resides

  

  BOOL Type: The bool value is true,false, no matter how many variables you create point to True,false, then he only has one in memory.

 # # # code block vs. small Data Pool # # # # #

# Pycharm executes the following code by running the file: I1 = 1000i2 = 1000print (I1 is i2)  # result is True
Execute the following code in interactive mode:>>> I1 = 1000>>> i2 = 1000>>> print (I1 is I2) False

Why in the Pycharm and interactive environment, the output of the results will be different??? This involves the relationship between the code block and the small data pool.

python in the same code block , when initializing the object, will put the variable and the corresponding value into a dictionary of memory (similar to: dic={' name ' = ' he ' corresponding memory address}), if the following code again encountered the initialization of the object assignment, if there is the same value, Will go to the dictionary to look for, and then directly reuse, pointing to the same memory address.
Python adheres to the principle of small data pools when initializing objects in different blocks of code .

Refer to the above concept of code block, because a py file in Pycharm is a block of code, and in the interactive file, different two different code blocks, so there will be the above differences.

(iv) Python code II

  At the beginning of the code, look back to the previous blog content.

###########################################################################################

From the previous content, we can draw the following conclusions:

(1) There is no mutual recognition between the codes.

(2) Because Unicode is too wasteful for resources, it must not be encoded in the form of Unicode in the network transmission, local storage.

###########################################################################################

Prerequisite Notes:

    In the python3.x version, the encoding of the string is Unicode and the others are utf-8.

  

However, there is a problem, then the string as a network transmission, local storage data type, encoding type Unicode, is not allowed. What's going to happen?

At this point, there is a basic data type of bytes in Python. The bytes type is comparable to the STR type of the twin brothers, Str type all methods, bytes types can be used.

# # # bytes types differ from str types? ###

For English:

Str:

form of expression: S1 = ' hehehe '

Encoding type: Unicode

bytes

Representation: B1 = B ' hehehe '

Encoding type: Non-Unicode

For Chinese:

Str:

form of expression: S1 = ' ah hehe '

Encoding type: Unicode

bytes

Representation: B1 = B ' \xe5\x91\xb5\xe5\x91\xb5\xe5\x91\xb5 '

Performance type: Non-Unicode

Conclusion:
#为什么需要同时拥有str与bytes? Because Chinese in the bytes is displayed in 16 binary, not easy to read. So what is the need for both STR and BYTES#STR types and bytes types to use? When the local storage/network Socekt is transmitted, the bytes type is used because the string encoding is Unicode. Other types with str

  # # # bytes type and str type conversion? ###

"Str and bytes conversion ' str----> bytes    encode () encode s1 = ' Alex ' B1 = S1.encode (' utf-8 ') print (B1,type (B1))
#bytes---->str    decode () decoding B1 = B ' \xe5\x91\xb5\xe5\x91\xb5\xe5\x91\xb5 ' S1 = B1.decode (' utf-8 ') print (S1) > >> OH

  

  

  

  

DAY6 python code block, small data pool detailed

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.