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: incorrect "'yield' outside function" error
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, willemb
Priority: low Keywords:

Created on 2004-11-04 11:25 by willemb, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg22994 - (view) Author: Willem Broekema (willemb) Date: 2004-11-04 11:25
In fact, the yield _is_ inside a function in the
following case:

>>> def f():
...   a = 3
...   exec("yield a")
...
>>> g = f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in f
  File "<string>", line 1
SyntaxError: 'yield' outside function
msg22995 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-11-05 17:12
Logged In: YES 
user_id=80475

Statements in an exec get interpreted and executed as a unit
and cannot affect surrounding control flow.  For example:
   for i in range(10):
       exec('continue')   # won't work

This is an intrinsic limitation and not a bug.  Perhaps the
docs could be improved though.
msg22996 - (view) Author: Willem Broekema (willemb) Date: 2004-11-06 21:40
Logged In: YES 
user_id=158280

It's the text of the error message that might be confusing;
something like "'yield' not allowed in 'exec'" would be better.
msg22997 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-11-06 22:21
Logged In: YES 
user_id=80475

Sorry.  No dice.
When you really understand what is happening,
the error message is dead-on.  You need to 
shift your mental model a bit.

The statements are executed without knowledge
of their surrounding context, only the dictionaries
can get shared.

When exec'd there is not enough information to
alter the message.
History
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 41121
2004-11-04 11:25:36willembcreate