A string conversion evaluates the contained expression list and converts the resulting
object into a string according to rules specific to its type.
If the object is a string, a number,
, or a tuple, list or dictionary containing only
objects whose type is one of these, the resulting string is a valid Python expression which
can be passed to the built-in function
to yield an expression with the same value
(or an approximation, if floating point numbers are involved).
(In particular, converting a string adds quotes around it and converts “funny” characters
to escape sequences that are safe to print.)
Recursive objects (for example, lists or dictionaries that contain a reference to
themselves, directly or indirectly) use
to indicate a recursive reference, and the result
cannot be passed to
to get an equal value (
will be raised instead).
The built-in function
performs exactly the same conversion in its argument as
enclosing it in parentheses and reverse quotes does. The built-in function
a similar but more user-friendly conversion.
5.2.10. Yield expressions
yield_atom ::= "(" yield_expression ")"
yield_expression ::= "yield" [expression_list]
New in version 2.5.
expression is only used when defining a generator function, and can only be
used in the body of a function definition. Using a
expression in a function definition
is sufficient to cause that definition to create a generator function instead of a normal
When a generator function is called, it returns an iterator known as a generator. That
generator then controls the execution of a generator function. The execution starts when
one of the generator’s methods is called. At that time, the execution proceeds to the first
expression, where it is suspended again, returning the value of
generator’s caller. By suspended we mean that all local state is retained, including the
current bindings of local variables, the instruction pointer, and the internal evaluation
stack. When the execution is resumed by calling one of the generator’s methods, the
function can proceed exactly as if the
expression was just another external call.
The value of the
expression after resuming depends on the method which resumed