Re: the usage of 'yield' keyword
Dave Angel wrote: def find(root): for pdf in os.walk(root, topdown=False): for file in pdf[2]: yield os.path.join(pdf[0],file) At the risk of nitpicking, I think that a modicum of tuple-unpacking would aid readability here: code for dirpath, dirnames, filenames in os.walk (root, topdown=False): for filename in filenames: yield os.path.join (dirpath, filename) /code TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
Peng Yu pengyu...@gmail.com writes: http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 Try the sections on iterators, generators and generator expressions in the tutorial: http://docs.python.org/tutorial/classes.html#iterators -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
Tim Golden wrote: div class=moz-text-flowed style=font-family: -moz-fixedDave Angel wrote: def find(root): for pdf in os.walk(root, topdown=False): for file in pdf[2]: yield os.path.join(pdf[0],file) At the risk of nitpicking, I think that a modicum of tuple-unpacking would aid readability here: code for dirpath, dirnames, filenames in os.walk (root, topdown=False): for filename in filenames: yield os.path.join (dirpath, filename) /code TJG /div Absolutely, readability is paramount. But in my defense, I was trying to make the minimal change necessary to the code that had been already posted, just to turn it into a generator. I didn't even notice why the pdf name was used till you pointed out the expansion. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
On Oct 13, 6:41 pm, Peng Yu pengyu...@gmail.com wrote: http://docs.python.org/reference/simple_stmts.html#grammar-token-yiel... The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 What do you think about that Peng?!? -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
On Oct 15, 9:48 pm, Sean DiZazzo half.ital...@gmail.com wrote: On Oct 13, 6:41 pm, Peng Yu pengyu...@gmail.com wrote: http://docs.python.org/reference/simple_stmts.html#grammar-token-yiel... The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 What do you think about that Peng?!? Please share your thoughts, as this list is a learning experience for everyone involved. We can learn from your thoughts/experiences as well. Cheers and well being, ~Sean -- http://mail.python.org/mailman/listinfo/python-list
the usage of 'yield' keyword
http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
Hello, I think that the best information available on the subject is the following: http://www.dabeaz.com/generators/ http://www.dabeaz.com/coroutines/ Best regards, Javier 2009/10/14 Peng Yu pengyu...@gmail.com: http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
Peng Yu wrote: http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 Let's make this as simple as possible. def g(x): ...yield x ... p = g(4) type(p) type 'generator' type(type(p)) type 'type' So there you have it. Generator is an instance of 'type', hence a new-style class, even though it is returned by a simple looking function. p.next() 4 What were you expecting? type(g) type 'function' g is a function that returns a generator. The yield statement does two things. It states what is to be returned by the generator's next() method and it also defines g as a function that returns a generator. -- Stephen Fairchild -- http://mail.python.org/mailman/listinfo/python-list
Re: the usage of 'yield' keyword
Peng Yu wrote: http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt The explanation of yield is not clear to me, as I don't know what a generator is. I see the following example using 'yield'. Could somebody explain how 'yield' works in this example? Thank you! def brange(limit): i = 0 while i limit: yield i i += 1 Informally, a generator is a convenient syntax for writing an iterator. What the above does is match xrange() behavior for single argument, but without any limitation on the size or type of the limit parameter. A little too much detail: When a function has the keyword yield within it, the function works much differently than normal functions. When it's called as a function, it returns a special object called an iterator. That iterator has a method called next(), which when called executes a piece of this function. It executes the function until it encounters a 'yield' statement. Then the yield value is returned from the next() function, but the function is still out-there, suspended. All local variables still exist, and it's just waiting for a chance to run some more. Next time next() method is called, the function resumes right after the yield, and runs until it gets to another yield (in this case the same yield, but with a new value). That new value is returned from the next(). Eventually, the function may 'return' instead of 'yield', as this one does when the limit value is reached. At that point, it generates a stop iteration exception (or some name like that). Now this sounds way too complex. But, if you study the definition of the for loop, you'll find a complementary description. When you write for item in iterable: The iterable is called (I'm blurring the details of when you need parens, and when you don't), and next() is called repeatedly, with the results of next() being assigned to 'item' until an exception occurs. If the exception is 'stop iteration' then the loop terminates normally. Bottom line is it is easy to write very complicated generators which are easy to use in simple for loops. And although you should also try to create an iterator object manually, for the experience, most of the time the generator function is much easier/quicker to write, and much easier to maintain and debug. Note that yield can do even more. I'm trying to describe the common usage, which is very convenient. And it can frequently be used to take a complicated generator and wrap it to make an easier-to-use generator that's more specific to a particular purpose. For example, if os.walk is a pain to use, and all you need is a sequence of all the files in a directory tree, def find(root): for pdf in os.walk(root, topdown=False): for file in pdf[2]: yield os.path.join(pdf[0],file) HTH DaveA -- http://mail.python.org/mailman/listinfo/python-list