Get the App
SLTechnology News&Howtos  ›  Servers  › 

Analysis on the use of Python yield Grammar

Shulou Source: shulou.com Published: 2022-06-01 02:25:06 09月25日 Update

The use of Python yield grammar analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

At the beginning of # cases:-

Def test_yield1 ():

Print'I am make test_yield1 function as an iterator...'

Yield

# call test_yield1 () directly

Test_yield1 ()

# there is no output, because the execution of the test_yield1 () function only constructs an iterator

# is equivalent to only writing a for loop, not writing the code logic of the for loop

# iterative method 1:

For i in test_yield1 ():

Pass

# found output, because there is a yield field in the test_yield1 function, so the test_yield1 function becomes a

# iterable generator constructor, so the iterative method can be used to output the results.

# iterative method 2:

It = test_yield1 ()

It.next ()

The execution results of iterative method 1 and iterative method 2 are the same:

# but an error will be reported if you use the following method

It = test_yield1 ()

It.next ()

It.next ()

# error StopIteration, this is why yield only constructs an iterative object once, so when it is executed the second time, there are no elements left to iterate, so an error is reported

# at the end of the case:-

# case two begins:-

# follow example 1 above to create a second iterative function object:

Def test_yield2 ():

Print'I am test_yield2 and execute once yiled...'

Yield

Print'I am test_yield2 and execute twice yiled...'

Yield

# execute directly without output

Test_yield2 ()

# iterative method 1:

For i in test_yield2 ():

Pass

# iterative method 2:

T = test_yield2 ()

T.next ()

T.next ()

The execution results of iterative method 1 and iterative method 2 are the same:

The end of case 2:-

From examples 1 and 2 above, we can see that if there is a yield in the function, the function will be specially compiled into a generator, and the function is an object that can be used for iteration.

Let's take a look at an example of example programming used in openstack:

It's not easy to understand at first glance, so next, simulate a demo:

# coding=utf-8

# the beginning of case III:-

Def test_yield3 (num_list):

For num in num_list:

If num%2 = = 0:

Yield num

Num_list = [1, 2, 3, 4, 5, 6]

T = test_yield3 (num_list)

Print t

For i in t:

Print i

Results:

# the end of case 3:-

Example 3 returns an iterable generator object that contains only an even number through the yield syntax. So the examples in openstack are also easy to understand.

Is to add obj with a self._filter_one (obj, filter_properties) value of true to the iterative object. This completes the filtering of hosts.

After reading the above, have you mastered the method of using and analyzing Python yield grammar? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Iterations methods functions objects results output syntax generators generation analysis examples content reason that is more this is a problem loop helpless for this. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Shulou Information Docker OPPO Reno