yield usage as statement or expression - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: yield usage as statement or expression (/thread-40962.html) |
yield usage as statement or expression - akbarza - Oct-21-2023 hi the below code is in site:https://realpython.com/introduction-to-python-generators/#understanding-the-python-yield-statement code: # from: https://realpython.com/introduction-to-python-generators/ \ #understanding-the-python-yield-statement def is_palindrome(num): # Skip single-digit inputs if num // 10 == 0: return False temp = num reversed_num = 0 while temp != 0: reversed_num = (reversed_num * 10) + (temp % 10) temp = temp // 10 if num == reversed_num: return True else: return False def infinite_palindromes(): num = 0 while True: if is_palindrome(num): print(num) i = (yield num) if i is not None: num = i num += 1 if __name__ == "__main__": pal_gen = infinite_palindromes() for i in pal_gen: digits = len(str(i)) if digits == 5: pal_gen.throw(ValueError("We don't like large palindromes")) pal_gen.send(10 ** (digits))On the above page is written: Quote:As of Python 2.5 (the same release that introduced the methods you are learning about now), yield is an expression, rather than a statement. Of course, you can still use it as a statement. But now, you can also use it as you see in the code block above, where i takes the value that is yielded. This allows you to manipulate the yielded value. More importantly, it allows you to .send() a value back to the generator. When execution picks up after yield, i will take the value that is sent..I before wrote a threat about statement and expression and asked about them:https://python-forum.io/thread-40934.html I have saw before usage of yield like yeild numbut in the above code there is i = (yield num). which of them are statements and which are expressions. also, plz, explain the above text that has been written on the mentioned page. why the line if i is not None: is used? explain about: pal_gen.send(10 ** (digits))thanks. RE: yield usage as statement or expression - deanhystad - Oct-21-2023 From https://docs.python.org/3/whatsnew/2.5.html#pep-342-new-generator-features Quote:In Python 2.3, yield was a statement; it didn’t return any value. In 2.5, yield is now an expression, returning a value that can be assigned to a variable or otherwise operated on:So yield is always an expression, with or without the surrounding parenthesis. You may need parenthesis for syntactic reasons. Later in same document: Quote:Values are sent into a generator by calling its send(value) method. The generator’s code is then resumed and the yield expression returns the specified value. If the regular next() method is called, the yield returns None.In your example, "i" is None when next(pal_gen) is called by the for loop, and it has some number when calling pal_gen.send(). This explains why the print statement is in the generator. The main loop only sees half the palindromes. To demonstrate I moved the print statement into the mainloop. def is_palindrome(num): # Skip single-digit inputs if num // 10 == 0: return False temp = num reversed_num = 0 while temp != 0: reversed_num = (reversed_num * 10) + (temp % 10) temp = temp // 10 if num == reversed_num: return True else: return False def infinite_palindromes(): num = 0 while True: if is_palindrome(num): i = (yield num) if i is not None: num = i num += 1 if __name__ == "__main__": pal_gen = infinite_palindromes() for i in pal_gen: print("next", i) digits = len(str(i)) if digits == 5: pal_gen.throw(ValueError("We don't like large palindromes")) print("send", pal_gen.send(10 ** (digits))) The palindrome generator runs twice for each iteration of the main loop, and ignores the palindromes returned when pal_gen.send() is called.
RE: yield usage as statement or expression - akbarza - Oct-22-2023 hi deanhystad I read the section related to generator from https://docs.python.org/3/whatsnew/2.5.h...r-features the below code is in it(simpler and shorter than what I posted before here.). # from:https://docs.python.org/3/whatsnew/2.5.html#pep \ # -342-new-generator-features def counter (maximum): i = 0 while i < maximum: val = (yield i) # If value provided, change counter if val is not None: i = val else: i += 1 if __name__== "__main__": it = counter(5) print( next(it)) print( next(it)) print( it.send(3)) # attention to send print( next(it)) print( next(it))I ran the above code with idle debugger line by line two times. after running the line val=(yield i), why the value of val is None. if this line means that the value of i is assigned to val variable? in the line print(it.send(3)), the value of 3 is assigned to i or val? I also saw in the introduced reference by you that is written: Quote:yield will usually return None...I do not understand this.plz, explain assume i=2 and maximum=3 and now we are in line val=(yield i). after running this line, what is the value of val variable? thanks for the last reply RE: yield usage as statement or expression - deanhystad - Oct-22-2023 If the generator resumes because of next(), the yield value will be None. If the generator resumes because of send(x), the yield value will be x. RE: yield usage as statement or expression - akbarza - Oct-23-2023 (Oct-22-2023, 06:34 PM)deanhystad Wrote: If the generator resumes because of next(), the yield value will be None. If the generator resumes because of send(x), the yield value will be x. hi I read your reply and thanks for replying. as I said I read the reference that you introduced and also I ran (by idle debugger)all the code in this thread at least twice for each( i ran some 5 times), but I sense I do not understand gernerator and send. can you mention another site or reference? thanks RE: yield usage as statement or expression - Gribouillis - Oct-23-2023 (Oct-23-2023, 11:16 AM)akbarza Wrote: can you mention another site or reference?An excellent and classical reference about generators and coroutines is David Beazley's talk(s) about this subject. |