In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.