Re: [Tutor] Difference between 'yield' and 'print'

2007-01-17 Thread Kent Johnson
Luke Paireepinart wrote:

 Refer to http://docs.python.org/ref/yield.html for information about yield.
 Disclaimer: The following information I'm going to present you I came up 
 with after reading that short explanation of 'yield' so it may not be 
 exactly correct.

There is a longer explanation here:
http://www.python.org/doc/2.2.3/whatsnew/node5.html

 So you could think of the loop like this:
 gen = some_function(5) #create the generator
 while 1:
 try:
print gen.next()
 except: #we ran out of things to generate (the generator stopped 
 yielding results -- supposedly it raises an error here?)
print End of generator.
break
 
 Anyway, I've never used them but that sounds like a reasonable way for 
 them to work.

Yes, that is pretty much what is going on. The generatory will raise 
StopIteration when there are no more values.

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Difference between 'yield' and 'print'

2007-01-16 Thread raghu raghu

Is there any difference between yield and print in python script?i have
written a script based on fibonacci series where in i used yield and print
in two different scripts:
the script is given below:
def fib(n):
   a,b = 0,1
   while a=n:
  print a
  a,b = b,a+b

for x in fib(4):
   print  x. When i executed this script i am getting this error:
Typeerror:nonetype is not iterable
But the same script if 'print' is replaced with 'yield' inside while loop it
is executing properly without any type errors
could any one reply why print cant be used instead of yield and why error is
generated?

--

  Vanam
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor