Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New example: yield from return #136

Closed
scidam opened this issue Aug 28, 2019 · 2 comments
Closed

New example: yield from return #136

scidam opened this issue Aug 28, 2019 · 2 comments
Assignees

Comments

@scidam
Copy link
Contributor

scidam commented Aug 28, 2019

Let consider the following function:

def my_func(k):
    if k == 3:
        return ["A string..."]
    else:
        yield from range(k)

What would be expected behavior, if we called

list(my_func(3))

Instead of exected output ["A string..."], we would get an empty list []!

The same behaviour is true, if we replace yield from with a loop, e.g.

def my_func_loop(k):
    if k == 3:
        return ["A string..."]
    else:
        for j in range(k):
            yield j

When yield from has come (Python 3.3+), it becomes possible to use return (with values) inside generators. The essence of such behavior becomes clear from PEP380. However, even being explainable from PEPs, this issue produces very unintuitive behavior.
Probably, this example could be included to this project. I can prepare a pull request for this.

@satwikkansal
Copy link
Owner

Hey @scidam

Thanks for suggesting this. It seems like a great addition to the collection. Do let me know when you open a PR for this, looking forward to it :)

@scidam
Copy link
Contributor Author

scidam commented Sep 13, 2019

Hello @satwikkansal! I opened a pull request for this snippet #138

@scidam scidam closed this as completed Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants