Re: [Tutor] generators
To Joel's and Wesley's valuable comments I add: Calling a generator function returns a /generator object/. >>> def x(n): ... for i in range(n): yield i ... >>> y = x(3) >>> print y A generator object can be used instead of some other "iterable" (e.g.) in for statements. >>> for i in y:print i 0 1 2 x in this case is equivalent to xrange() with exactly 1 argument. There is more that can be said regarding x.next(). x.send(), raise StopIteration but I've said enough for now. -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generators
> On Tue, Apr 3, 2012 at 2:38 PM, mike jackson wrote: > I am trying understand python and have done fairly well, So for it has been > easy to learn and is concise. However I seem to not quite understand the use > of a generator over a function(I am familiar with functions [other languages > and math]). To me (excepting obvious syntax differences) a generator is a > function. Why should I use a generator instead of a function or vice versa? > Is perhaps specfic uses it was created to handle? A great web page with good > examples would be nice. Of course if you can sum it up rather easy then by > all means go ahead. dave beazley's lectures are *awesome*, and even more so if you can attend them in-person. below are my comments on generators off the top of my head: 1. syntactically, generators are merely functions with one or more "yield" statements/expressions 2. users of generators will see them primarily as "advanced" iterators, because they yield individual values until such an iterator has been exhausted (StopIteration). 3. creators of generators will see them more like functions that you can pause, "return" some intermediate value, then be resumable/resumed later. the C language has the concept of a "static function" where variables can maintain their values across function calls. while being "nice," it's not nearly as powerful as being able to save the values *and* the entire state of execution at the time the function is paused then resume right where it left off later, hence the comparisons with co-routines (which are even more independent threads of execution). 4. "generator expressions" are the lazy evaluation form of list comprehensions, and better for memory because of that. they'll behave just like generators but can be defined easily on a single line, just like normal listcomps. 5. i made a quick 5-minute video introducing Python developers to generators... it's a very ad hoc and informal session during one of my Python courses that a student recorded. if interested in viewing it, you can find it half-way down http://cyberwebconsulting.com hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "A computer never does what you want... only what you tell it." wesley chun : wescpy at gmail : @wescpy/+wescpy Python training & consulting : http://CyberwebConsulting.com "Core Python" books : http://CorePython.com Python blog: http://wescpy.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generators
On Tue, Apr 3, 2012 at 2:38 PM, mike jackson wrote: > I am trying understand python and have done fairly well, So for it has been > easy to learn and is concise. However I seem to not quite understand the use > of a generator over a function(I am familiar with functions [other languages > and math]). To me (excepting obvious syntax differences) a generator is a > function. Why should I use a generator instead of a function or vice versa? > Is perhaps specfic uses it was created to handle? A great web page with good > examples would be nice. Of course if you can sum it up rather easy then by > all means go ahead. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor A generator function is a special kind of function that uses the 'yield' statement to return a value. The next time the function is called, it starts up from the place following the yield statement. They are useful in producing the next value in a computed sequence of values without having to compute the whole sequence at one go. Here is a great tutorial about things you can do with generators: http://www.dabeaz.com/generators/ Here is some simple code with results below #! /usr/bin/env python """ generator vs normal function""" """ a 'Normal' function""" def n(r): v = [] for i in range(r): v.append(i*2) return v """ A generator function""" def g(r): for i in range(r): yield i*2 print n(3) for i in g(3): print i generated_list = [i for i in g(3)] print generated_list [0, 2, 4] 0 2 4 [0, 2, 4] -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generators
Hey Mike The following link should help you. http://www.dabeaz.com/generators/ . Cool slide deck with examples from David Beazley's explanation of generators. -A On Tue, Apr 3, 2012 at 11:38 AM, mike jackson wrote: > I am trying understand python and have done fairly well, So for it has been > easy to learn and is concise. However I seem to not quite understand the use > of a generator over a function(I am familiar with functions [other languages > and math]). To me (excepting obvious syntax differences) a generator is a > function. Why should I use a generator instead of a function or vice versa? > Is perhaps specfic uses it was created to handle? A great web page with good > examples would be nice. Of course if you can sum it up rather easy then by > all means go ahead. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generators
On Tuesday 24 July 2007 13:11, Kent Johnson wrote: > 'while 1' is optimized to just a jump - the compiler recognizes that the > test is not needed and skips it. 'while True' requires looking up the > value of the name 'True' and testing it. This may seem counter intuitive, but the reason for this (for anyone puzzled) is because True is a name for the value representing boolean true, rather than a keyword for the value. If it were a keyword for the value, then like "1" it could not change in value. However since it isn't, it can. For example, True can become false as the following demonstrates, so you do have to do those steps of looking up the value of the name "True" and test it: Python 2.5 (r25:51908, Nov 27 2006, 19:14:46) [GCC 4.1.2 20061115 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True = 10 >>> while True: ... print True ... True = True -1 ... 10 9 8 7 6 5 4 3 2 1 >>> Rebinding True to anything else is a bad idea, but doable :-) If you change True to "True" you get the same behaviour - python detects an immutable object in the condition and optimises it: >>> dis.dis(compile('while "True": pass', '', 'exec')) 1 0 SETUP_LOOP 3 (to 6) >>3 JUMP_ABSOLUTE3 >>6 LOAD_CONST 0 (None) 9 RETURN_VALUE Regards, Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generators
Tiger12506 wrote: >> I am new to Python but not new to programming languages. I have seen this >> "while 1" a lot. In fact in other languages I would expect "while 1" to >> be >> the same as "while TRUE". The predefined constants True and False were not added to Python until version 2.3. Before that it was common to write 'while 1'. In fact 'while 1' is more efficient, too. Compare: In [5]: dis.dis(compile('while 1: pass', '', 'exec')) 1 0 SETUP_LOOP 3 (to 6) >>3 JUMP_ABSOLUTE3 >>6 LOAD_CONST 0 (None) 9 RETURN_VALUE In [6]: dis.dis(compile('while True: pass', '', 'exec')) 1 0 SETUP_LOOP 12 (to 15) >>3 LOAD_NAME0 (True) 6 JUMP_IF_FALSE4 (to 13) 9 POP_TOP 10 JUMP_ABSOLUTE3 >> 13 POP_TOP 14 POP_BLOCK >> 15 LOAD_CONST 0 (None) 18 RETURN_VALUE 'while 1' is optimized to just a jump - the compiler recognizes that the test is not needed and skips it. 'while True' requires looking up the value of the name 'True' and testing it. Old habits die hard, especially when they are more efficient and require less typing :-) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generators
Tiger12506 wrote: >> I am new to Python but not new to programming languages. I have seen this >> "while 1" a lot. In fact in other languages I would expect "while 1" to >> be >> the same as "while TRUE". I have used that in other languages, but in the >> definition below I would expect the "yield b..." to continue on until a >> "break". So the question is, how does this work without going into an >> infinite loop without a break? >> >> Jeff >> > > while 1: and while TRUE: mean the same thing. > The thing is - it is an infinite loop. This should be while True: not while TRUE: unless you set TRUE = True beforehand, since Python is case-sensitive. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generators
> I am new to Python but not new to programming languages. I have seen this > "while 1" a lot. In fact in other languages I would expect "while 1" to > be > the same as "while TRUE". I have used that in other languages, but in the > definition below I would expect the "yield b..." to continue on until a > "break". So the question is, how does this work without going into an > infinite loop without a break? > > Jeff while 1: and while TRUE: mean the same thing. The thing is - it is an infinite loop. >>> a = fibonacci() >>> a.next() 1 >>> No matter how many times you call a.next(), it will continue to return numbers. You are being fooled by the yield statement. Generators are special objects. The yield statement is ~~ difficult to translate into computer terms. My first impulse is to compare it to an interrupt, but you might not know what that is. The best way to explain it is to litter that example with print statements. Here: def fibonacci(): a = 1 print "a = 1" b = 1 print "b = 1" print "Before yield a" yield a print "After yield a" while 1: print "Before yield b" yield b print "After yield b" a, b = b, a+b print "After calculation" >>> a = fibonacci() >>> a.next() a = 1 b = 1 Before yield a 1 >>> a.next() After yield a Before yield b 1 >>> a.next() After yield b After calculation Before yield b 2 >>> a.next() After yield b After calculation Before yield b 3 >>> JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generators
> Please forgive my instrusion with some simple questions. I don't have any > formal training in programming so I have to get some guidance to some > terminology from time to time. What is a generator and what is its > purpose? Think of a generator as a list whose contents haven't been finished yet. Essentially, it "generates" each successive element of the list on the fly. Here's a classic example of where a generator is required, as opposed to a list. def fibonacci(): a = 1 b = 1 yield a while 1: yield b a, b = b, a+b Because fibonacci numbers are an infinite sequence (you can never stop generating the numbers) it would be ridiculous to try to store all of them in a list. A generator is custom suited to problems like this. Oh, test this code this way -> >>> a = fibonacci() >>> a.next() 1 >>> a.next() 1 >>> a.next() 2 >>> for i in range(100): print a.next() 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 12586269025 20365011074 32951280099 53316291173 86267571272 139583862445 225851433717 365435296162 591286729879 956722026041 1548008755920 2504730781961 4052739537881 6557470319842 10610209857723 17167680177565 2890035288 44945570212853 72723460248141 117669030460994 190392490709135 308061521170129 498454011879264 806515533049393 1304969544928657 2111485077978050 3416454622906707 5527939700884757 8944394323791464 14472334024676221 23416728348467685 37889062373143906 61305790721611591 99194853094755497 160500643816367088 259695496911122585 420196140727489673 679891637638612258 1100087778366101931 1779979416004714189 2880067194370816120 4660046610375530309 7540113804746346429 12200160415121876738 19740274219868223167 31940434634990099905 51680708854858323072 83621143489848422977 135301852344706746049 218922995834555169026 354224848179261915075 573147844013817084101 927372692193078999176 1500520536206896083277 >>> Which provides the expected results for a fibonacci sequence. JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generators
Doug Glenn wrote: > Greetings all, > > Please forgive my instrusion with some simple questions. I don't have > any formal training in programming so I have to get some guidance to > some terminology from time to time. What is a generator and what is its > purpose? If you are that new to programming you probably don't need to understand generators. Why do you ask? Here is a short introduction: http://personalpages.tds.net/~kent37/kk/4.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor