The Wayback Machine - https://web.archive.org/web/20211128051549/https://stackoverflow.com/posts/12068827/revisions
Edited question title and removed language tag.
Link
martineau
  • 106.4k
  • 22
  • 147
  • 261

What is thea "yield" statement in a Python function?

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Possible Duplicate:
The Python yield keyword explainedThe 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.

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.

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.

Improved formatting
Source Link
Alexander
  • 8.5k
  • 3
  • 49
  • 58

Possible Duplicate:
The Python yield keyword explained

Can someone explain to me what does the yield statement doactually 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.

Possible Duplicate:
The Python yield keyword explained

Can someone explain to me what does the yield statement do 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.

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.

insert duplicate link
Source Link
Loading
Post Closed as "exact duplicate" by lvc, user166390, Scott Griffiths, monkut, user395760 of
Formatted properly
Source Link
avasal
  • 13.3k
  • 4
  • 29
  • 46
Loading
edited title
Link
user166390
user166390
Loading
Source Link
Dewclaw
  • 191
  • 1
  • 5
Loading