Python3.4 yield and yield from

Source: Internet
Author: User

Yield syntax

This log is primarily a reference to the Python 3:using "yield from" in generators.

The previous blog post describes yield the use, now in writing an example:

     class Node:         def __init__(self,value):Self.left = [] Self.value = value Self.right = [] def node_iterate(self):             forValueinchSelf.child_iterate (self.left): v =yieldValue Print (' Left V is%s '% v) v =yieldSelf.value forValueinchSelf.child_iterate (self.right): v =yieldValue Print (' Right V is%s '%s def child_iterate(self,nodes):             forNodeinchNodes:input_value =yieldNode.value Print (' input value is%s '% input_value) def main():Root = Node (0) Root.left = [Node (i) forIinch[1,2,3]] Root.right = [Node (i) forIinch[4,5,6]] it = root.node_iterate () total = It.send (None) while True:Try: ans = it.send (total) + + ansexceptStopiteration: Break                Else: Print (' ans is%s '% ans) print (' Total is%s '% total)if__name__ = =' __main__ ': Main ()

Run the above code:

Yield version results for "title=" ">

Found in the function child_iterate() of the print(‘input_value is %s‘ % input_value) output is always ' input_value is None '. Thought that when the execution to input_value = yield value , the method is directly yield , the next time directly from the next sentence to start execution.

Therefore, if you want to use input_value the value of the operation, it is not possible.

However node_iterate() , the value can be used in the v = yield value operation. But the observed v value, which is equal to the root.send(total) value passed in total . And the value in the while statement is the value ans that you want-the value of the node.

The node_iterate() value in is v not the desired value, plus this sentence is obviously superfluous. At this point you can use yield from the syntax. Modify node_iterate() the code in:

    def node_iterate(self):        yieldfrom self.child_iterate(self.left)        yield self.value        yieldfrom self.child_iterate(self.right)

After saving the code in the run, you will find that child_iterate() input_value there is a value in it:

Yield from run result "title=" ">

If you want to use only yield , you can use the send() and next() methods to control the operation of the generator.

You can also continue to modify the code:

     class Node:         def __init__(self,value):Self.left = [] Self.value = value Self.right = [] def process(self):v =yieldSelf.value Print (' V is%s '% v) def child_iterate(self,nodes):             forNodeinchNodesyield  fromNode.process () def node_iterate(self):            yield  fromSelf.child_iterate (Self.left) self.process ()yield  fromSelf.child_iterate (self.right) ...

Does the code look much cleaner? Use yield from a code that can write less.

Oneself to yield and yield from still not very familiar, wait until very ripe, good end of it, now first use to say. Because if asyncio , aiohttp and aiomysql these three libraries, the code cannot leave yield from .

Come on!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python3.4 yield and yield from

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.