![]() |
Python curious behavior - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: Python curious behavior (/thread-19989.html) |
Python curious behavior - scidam - Jul-23-2019 Experimenting with generators and functions I tried the following example: def myfunc(k): if k == 3: return ["A string..."] else: yield from range(k)What is expected behavior of this function, if we call it, e.g., as follows list(myfunc(3)) ?list(myfunc(3)) Why does this function return an empty list? This is quite unintuitive behavior of Python, because list(["A string..."]) should return ["A string..."] . This behavior remains true not only if we are using yield from construction, e.g.def myfunc1(k): if k == 3: return ["A string..."] else: for j in range(k): yield jAnd we still got unintuitive result: list(myfunc1(3)) In Python 2.x, a try to define such function (myfunc1 ) yields SyntaxError.We can find an explanation of this behavior in PEP255, Quote: A generator function can also contain return statements of the form: Nevertheless, when yield from has come (Python 3.3+, see PEP380), it becomes possible to use return (with values) inside generators. The essence of such behavior becomes clear from PEP380:Quote:2. In a generator, the statement Even being explainable from PEPs, this issue produces very unintuitive behavior, and probably a special warning needs to be raised (e.g. SyntaxWarning or something else) when one tries to execute such construction. What do you think about this behavior? Do Python needs to raise a special warning to users, when they try to use both return and yield/yield from in the same function/generator's body?
RE: Python curious behavior - DeaD_EyE - Jul-23-2019 No, because the construct is useful. You have the ability to stop the iteration. It's not used to get an additional value out. Return a value via return is different use case and need the try-except clause. RE: Python curious behavior - scidam - Jul-23-2019 (Jul-23-2019, 07:09 AM)DeaD_EyE Wrote: Return a value via return is different use case and need the try-except clause.May be I something misunderstood, but according to docs this could not be handled via try-except "except that, as currently, the exception cannot be caught by except clauses within the returning generator.". (Jul-23-2019, 07:09 AM)DeaD_EyE Wrote: No, because the construct is useful.I just told about raising a warning, not an exception. This is only polite way to point user's attention on using return and yield in the same place. Such warning passed to stdout would be informative, I think.This warning wouldn't break any existing code. |