Iterators & generators (RE: Real Problems with Python)
William Tanksley
wtanksle at hawking.armored.net
Mon Feb 14 00:47:15 EST 2000
More information about the Python-list mailing list
Mon Feb 14 00:47:15 EST 2000
- Previous message (by thread): Iterators & generators (RE: Real Problems with Python)
- Next message (by thread): Iterators & generators (RE: Real Problems with Python)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 14 Feb 2000 02:02:55 GMT, Samuel A. Falvo II wrote: >In article <000001bf768e$48e40580$45a0143f at tim>, Tim Peters wrote: >>class BinTree: >> # with members .left and .right of type BinTree (& None means none), >> # and .data of an arbitrary type >> ... >> def traverse_post(self): >> for child in self.left, self.right: >> if child is not None: >> suspend child.traverse_post() >> suspend self.data >>b = BinTree() >>... >>for leaf in b.traverse_post(): >> process(leaf) >I'm sorry, but I can't follow this code at all. What are the precise >semantics of suspend here? How does it return? Sam, imagine that instead of 'suspend' Tim had written 'return'. You see, then, that this would be a deeply recursive function which would return the data contained in the deepest node of the left-hand branch of the tree, right? (Ignore the fact that such recursion would be rather silly -- thanks to the 'suspend' it's not the same.) Well, it so happens that this is precisely what this function (using suspend instead of return) returns the first time it's called. The second time it's called it continues what it was doing when it did the last suspend -- look at it. It's asking each of its children to do a post-order traversal, and after they finish, return their own data. In other words, it's doing a depth-first search and allowing you to treat the result as a sequence. Kind of. If you have more questions, call me; I teach better that way. Or you could learn Icon -- it's a really cool language, and lots of fun. Sather includes a similar construct, although (alas!) I never saw it as fun there. Perhaps I was jaded, or maybe Sather didn't do it as well. >KC5TJA/6, DM13, QRP-L #1447 -- -William "Billy" Tanksley, in hoc signo hack
- Previous message (by thread): Iterators & generators (RE: Real Problems with Python)
- Next message (by thread): Iterators & generators (RE: Real Problems with Python)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list