-1

Two questions/clarifications:

  1. Most people may ask the difference between yield and return.

     def square(n):
         for i in range(n):
             yield i**2
     for i in square(10000000000):
          print(i)
    

I understand this is a way to run the 'function' in a generator way. If we directly run print([i**2 for i in range(10000000000)]) or this way

def square():
    return [i**2 for i in range(10000000000)]

Python will run almost infinite time. However, if we run in the generator way, we will save a lot of memories, and Python can keep printing the squared numbers. In the generator, we directly print out the result we got and do not save it anywhere. However, in a function-return way, we will save the results into a list so it will waste a lot of memories and time. Am I correct about this?

  1. My another question is yield vs. print: if we want to save memories and hope to directly print out the results, then why don't we just use print to directly print out the result, like:

     def square(n):
         for i in range(n):
             print(i**2)
     square(10000000000)
    

Python will do the same thing as the yield-generator way, right? So what is the difference between yield and print? Thanks!

camille
  • 16,432
  • 18
  • 38
  • 60
Troy Bear
  • 55
  • 5
  • 2
    "Python will do the same thing as the yield-generator way, right?" -- No. `yield` doesn't print anything. You are using `print` in both pieces of code, not `yield` in one and `print` in the other. – John Coleman Jul 05 '21 at 23:12
  • Thank you. Basically, if I know the number of results I want to print, I can just use print. If I don't know the specific use cases, I will need the generator way like to use a function for different use cases, right? – Troy Bear Jul 05 '21 at 23:20
  • 1
    Tangentially: your use of "memories" rather than "memory" seems odd. It is likely that you only have one computer memory. In computer science, "memory" is seldom used in a plural from. – John Coleman Jul 05 '21 at 23:25
  • Does this answer your question? [What does the "yield" keyword do?](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do) – Life is complex Jul 05 '21 at 23:51

1 Answers1

1

Depends on the purpose and the caller.

When you are print-ing, you are calling a console writer in the loop. Thus print sends to a fixed and forced destination.

When you are yielding, you are not calling anything, but simply return-ing your output in gradual way to "any" caller which can take it and do whatever it like.

Dharman
  • 30,962
  • 25
  • 85
  • 135
S2L
  • 1,746
  • 1
  • 16
  • 20
  • Thank you. Basically, if I know the number of results I want to print, I can just use print. If I don't know the specific use cases, I will need the generator way like to use a function for different use cases, right? – Troy Bear Jul 05 '21 at 23:19
  • Correct. If you are creating a function to be used by others, you will use yield to send (generate) the results, so your caller can consume without high memory overhead. Print is typically for your own troubleshooting and testing your code. – S2L Jul 05 '21 at 23:20