Talk about Python's cold knowledge that is unknown

Share
/ 2019-04-22 / News

for infinite loop

for loop can be said to be a basic knowledge point. But if you let for write an infinite loop, would you write it?

This is an open question. Before looking down, I suggest you try it yourself and how you will answer it.

Okay, if you don't have ideas yet, take a look at the answers from an overseas MIT group of friends:

for i in iter(int, 1):pass

Is it hard? Iter also has this usage? Is this an infinite loop?

This is really a cold knowledge. If you look at the Chinese website, you may not find relevant information.

Fortunately, you can see the comment content in the py source code through the IDE, and introduce the very detailed usage.

The original iter has two ways to use it. Usually our cognition is the first one, turning a list into an iterator.

The second method, he receives a callable object, and a sentinel parameter. The first object will run until it returns the sentinel value.

that int , this is another knowledge point, int is a built-in method By looking at the comment, you can see that it has a default value of 0. You can type  int() on the terminal to see if it returns 0.

Because int() always returns 0, forever Can't return 1, so this for loop will have no end. It will run all the time.

Sample 1

# Python2.7>>> a = "Hello_Python">>> id(a)32045616>>> id("Hello" + "_" + "Python")32045616# Python3.7>>> a = "Hello_Python">>> id(a)38764272>>> id("Hello" + "_" + "Python")32045616

Sample 2

>>> a = "MING">>> b = "MING">>> a is bTrue# Python2.7>>> a, b = "MING!", "MING!">>> a is bTrue# Python3.7>>> a, b = "MING!", "MING!">>> a is bFalse

Sample 3

# Python2.7>>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'True>>> 'a' * 21 is 'aaaaaaaaaaaaaaaaaaaaa'False# Python3.7>>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'True>>> 'a' * 21  Is 'aaaaaaaaaaaaaaaaaaaaa'True

twice return

We all know that try…finally&hellip The usage of the statement, regardless of whether the try is normal execution or an exception, will eventually ensure that finally can be executed.

At the same time, we know that a function will end up as soon as it encounters the return function.

Based on the above two points, let's take a look at this example. What is the running process?

>>> def func():...     try:...&nbsp ;        return 'try'...     finally:...          return 'finally'...>>> func()'finally'

Amazing discovery, in The return in the try does not work.

The reason is that in the try…finally… statement, the return in the try will be ignored directly, because it is guaranteed to be able to execute.

small integer pool

See the example first.

>>> a = -6>>> b = -6>>> a is bFalse>>> a = 256>>> b = 256>>> a is bTrue>>> a = 257>>> b = 257>>> a is bFalse>>> a = 257; b = 257>>> a is bTrue

To avoid integers frequently requesting and destroying memory space, Python defines a small integer pool [-5, 256] These integer objects are built in advance, Will not be recycled by garbage.

The above code should be tested in the terminal Python environment. If you are testing in the IDE, this is not the case.

The last example,  and True?

Because when you assign the same value to two variables at the same time, the interpreter knows that the object has been generated, then it will reference the same An object. If it is divided into two, the interpreter does not know that the object already exists, and will reapply the memory to store the object.

intern mechanism

string type is one of the most commonly used data types in Python. The Python interpreter does this in order to improve the efficiency and performance of string usage. A lot of optimizations.

For example: the int (string-resident) technique is used in the Python interpreter to improve string efficiency.

What is the intern mechanism? The same string object will only save one copy, put it in a string savings pool, it is shared, of course, it can't be changed, it also determines The string must be immutable.

>>> s1="hello">>> s2= "hello">>> s1 is s2True# If there are spaces, the intern mechanism is not enabled by default>>> s1="hell o">>> s2 ="hell o">>> s1 is s2False# If a string is longer than 20 characters, the intern mechanism is not started>>> s1 =  "a" * 20>>> s2 = "a" * 20>>> s1 is s2True>>> s1 = "a" * 21>>> s2 = "a" * 21>>> s1 is s2False>>> s1 = "ab" * 10>>> s2 = "ab" * 10>>> s1 is s2True>>> s1 = "ab" * 11>>> s2 = "ab" * 11>>> s1 is s2False

Keywords: Function for python
Relevant articles recommend

Top suppliers

Response Time

24h

Response Rate

0%

Product:IC, CAPACITORS DIODES LED ....
1YRS

Response Time

24h

Response Rate

100.00%

Product:Photo Coupler,Optocoupler,IC,TOS,ON,AVAGO,SHARP,RENESAS,CT,EL,LTV,TI,NXP,ST
1YRS

Response Time

24h

Response Rate

100.00%

Product:Infrared Receiver Module,Infrared Emitting Diode,Silicon PIN Photodiode,Silicon Phototransistor,LED,Silicon Photocell,Hall Effect IC,Photoelectric switch,Photoelectric encoder,IC.
1YRS

Response Time

24h

Response Rate

100.00%

Product:KSD301 KSD9700 thermostat thermal protector fuse sensor
1YRS

Response Time

24h

Response Rate

0%

Product:IC,PCB,module,capacitor,resistor,diodes,LED,LCD
1YRS

Response Time

24h

Response Rate

100.00%

Product:Electronics components,ICs,MCU,RS-485/RS-422 Transceiver
9YRS
Subscribe News

Enter your email address:

Hot News

Contact Us

+86-755-83536845

One to One Customer Service

17190417227