Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yield statement question
#1
I have been reading up on the "yield" instead of "return" statement.
Arguably, I understand what it does.

On most sites however, they give the generic(?) example of how to
print the squares of the numbers from 1 to 10 using a def function with a while loop etc..
They need 10 lines of code for that yield example (even taking out the comment lines)
Anybody can print those squares in 2, let alone 1 line of code.

Question: could somebody describe - in words only - a situation where using "yield"
provides a huge benefit?

thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
"Yield" is useful in separating concerns. It separates the code that creates a collection of similar entities from the code that uses this collection. You can decide to print these items or store them in a container or whatever, but the function that generates the items doesn't need to know what you are going to do with them. Its sole responsibility is the production of these items.
Reply
#3
One use case is for generators that allow you to create potentially infinite sequences. Obviously, you can't store such things in memory, so this technique allows you to compute the next value on demand as you iterate. The idea is called "lazy evaluation" in general.
Reply
#4
OK, useful info thanks.
Maybe we could somehow experiment this in seeding a "Conway game of life"

thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
You know that John Conway died this spring of Covid 19?
Reply
#6
Simple answer: stacked generators result in processing pipelines

But are you ready to bend your mind? If yes then David Beazley - Generators the Final Frontier is waiting you.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Sep-26-2020, 03:00 PM)Gribouillis Wrote: You know that John Conway died this spring of Covid 19?

Yes I do.
He wrote the book !

As for " David Beazley - Generators the Final Frontier" : i'll need to look into that.
Sounds exciting Huh

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question of using not in if statement and .. akbarza 3 363 Feb-29-2024, 08:40 AM
Last Post: Gribouillis
  yield usage as statement or expression akbarza 5 752 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Yield generator weird output Vidar567 8 3,197 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 1,935 Oct-19-2020, 03:36 PM
Last Post: omm
  Problem about yield, please help!! cls0724 5 2,814 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  does yield support variable args? Skaperen 0 1,643 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,106 Jun-04-2019, 10:26 PM
Last Post: snippsat
  yield help chakox 5 3,208 Apr-13-2019, 09:42 PM
Last Post: chakox
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,797 Mar-21-2019, 08:54 PM
Last Post: mmoelle1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020