Python Forum
yield usage as statement or expression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
yield usage as statement or expression
#1
hi
the below code is in site:https://realpython.com/introduction-to-p...-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 num
but 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.
Reply


Messages In This Thread
yield usage as statement or expression - by akbarza - Oct-21-2023, 11:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  yield from akbarza 4 486 Apr-19-2024, 07:58 PM
Last Post: DeaD_EyE
  difference between statement and expression akbarza 6 997 Oct-18-2023, 07:40 AM
Last Post: akbarza
  Using list comprehension with 'yield' in function tester_V 5 1,389 Apr-02-2023, 06:31 PM
Last Post: tester_V
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,771 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Yield generator weird output Vidar567 8 3,394 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 2,070 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,600 Sep-26-2020, 05:18 PM
Last Post: DPaul
  Problem about yield, please help!! cls0724 5 3,002 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  Pass results of expression to another expression cmdr_eggplant 2 2,371 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  does yield support variable args? Skaperen 0 1,725 Mar-03-2020, 02:44 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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