This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: problem when using yield
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, xsmyqf
Priority: normal Keywords:

Created on 2020-04-21 17:05 by xsmyqf, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg366931 - (view) Author: xie (xsmyqf) Date: 2020-04-21 17:05
----------version1:--------------
def func1():
    a=0 
    b=10
    for i in range(4):
        result = yield a+100 if b>100 else (yield a)
        print(result)

f1 = func1()
print("value:%s" % next(f1))
print("--------------")
print("value:%s" % next(f1))
print("--------------")

----------version2--------------

def func1():
    a=0 
    b=10
    for i in range(4):
        result = (yield a+100) if b>100 else (yield a)
        print(result)

f1 = func1()
print("value:%s" % next(f1))
print("--------------")
print("value:%s" % next(f1))
print("--------------")


------------------problem------------------------
I think two version should behave same,but it did not.Do this will be treated as a bug ?
msg366933 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-04-21 17:58
Which part of the behaviour do you think is a bug?

In version1, you want to think of the yield expression as though it were bracketed like this:

    result = yield (a+100 if b>100 else (yield a))

With the particular values of a and b that you have, that statement is equivalent to this:

    result = yield (yield 0)

This yields two values to the consumer: a `0` from the inner yield, followed by a None (or whatever was "sent" into the generator, if applicable) for the out yield.

In version 2, the corresponding line simplifies to just

    result = yield 0

So no, I don't think these two versions of the code should behave the same, and I don't think there's any bug here.
msg366961 - (view) Author: xie (xsmyqf) Date: 2020-04-22 00:14
Okay,I agree with you.I did not get the priority.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84534
2020-04-22 00:14:52xsmyqfsetstatus: open -> closed

messages: + msg366961
stage: resolved
2020-04-21 17:58:22mark.dickinsonsetnosy: + mark.dickinson
messages: + msg366933
2020-04-21 17:05:16xsmyqfcreate