Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "yield" statement in a function? [duplicate]

Tags:

python

Possible Duplicate:
The Python yield keyword explained

Can someone explain to me what the yield statement actually does in this bit of code here:

 def fibonacci():
     a, b = 0, 1
     while True:
         yield a
         a, b = b, a+b

for number in fibonacci(): # Use the generator as an iterator; print number

What I understand so far is, we are defining a function finonacci(), with no parameters? inside the function we are defining a and b equal to 0 and 1, next, while this is true, we are yielding a. What is this actually doing? Furthermore, while yielding a? a is now equal to b, while b is now equal to a + b.

Next question, for number in fibonacci(), does this mean for every number in the function or what? I'm equally stumped on what yield and 'for number' are actually doing. Obviously I am aware that it means for every number in fibonacci() print number. Am I actually defining number without knowing it?

Thanks, sorry if I'm not clear. BTW, it's for project Euler, if I knew how to program well this would be a breeze but I'm trying to learn this on the fly.

like image 733
Dewclaw Avatar asked Aug 22 '12 08:08

Dewclaw


2 Answers

Using yield makes the function a generator. The generator will continue to yield the a variable on each loop, waiting until the generator's next() method is called to continue on to the next loop iteration.

Or, until you return or StopIteration is raised.

Slightly modified to show use of StopIteration:

>>> def fib():
...     a = 0
...     b = 1
...     while True:
...         yield a
...         a = b
...         b += a
...         if a > 100:
...             raise StopIteration
...
>>>
>>> for value in fib():
...     print value
...
0
1
2
4
8
16
32
64
>>>

>>> # assign the resulting object to 'generator'
>>> generator = fib()
>>> generator.next()
0
>>> generator.next()
1
>>> for value in generator:
...     print value
...
2
4
8
16
32
64
>>>
like image 119
monkut Avatar answered Oct 12 '22 09:10

monkut


Generators have a special property of being iterables which do not consume memories for their values.

They do this by calculating the new value, when it is required while being iterated.

i.e.

def f():
    a = 2
    yield a
    a += 1

for ele in f():
    print ele

would print

 2

So you are using a function as an iterable that keeps returning values. This is especially useful when you require heavy memory usage, and so you cannot afford the use of a list comprehension

i.e.

li = [ele*10 for ele in range(10)]

takes 10 memory spaces for ints as a list

but if you simple want to iterate over it, not access it individually

it would be very memory efficient to instead use

def f():
    i=0
    while i<10
        yield i*10
        i += 1

which would use 1 memory space as i keeps being reused

a short cut for this is

ge = (i*10 for i in range(10))

you can do any of the following

for ele in f():

for ele in li:

for ele in ge:

to obtain equivalent results

like image 45
acid_crucifix Avatar answered Oct 12 '22 09:10

acid_crucifix