blob: 1cff8a52df959d555d8174e1c00e1a2198d2bcc7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2.. _expressions:
3
4***********
5Expressions
6***********
7
Georg Brandl4b491312007-08-31 09:22:56 +00008.. index:: expression, BNF
Georg Brandl116aa622007-08-15 14:28:22 +00009
Brett Cannon7603fa02011-01-06 23:08:16 +000010This chapter explains the meaning of the elements of expressions in Python.
Georg Brandl116aa622007-08-15 14:28:22 +000011
Georg Brandl116aa622007-08-15 14:28:22 +000012**Syntax Notes:** In this and the following chapters, extended BNF notation will
13be used to describe syntax, not lexical analysis. When (one alternative of) a
14syntax rule has the form
15
16.. productionlist:: *
17 name: `othername`
18
Georg Brandl116aa622007-08-15 14:28:22 +000019and no semantics are given, the semantics of this form of ``name`` are the same
20as for ``othername``.
21
22
23.. _conversions:
24
25Arithmetic conversions
26======================
27
28.. index:: pair: arithmetic; conversion
29
Georg Brandl116aa622007-08-15 14:28:22 +000030When a description of an arithmetic operator below uses the phrase "the numeric
Georg Brandl96593ed2007-09-07 14:15:41 +000031arguments are converted to a common type," this means that the operator
Raymond Hettingeraa7886d2014-05-26 22:20:37 -070032implementation for built-in types works as follows:
Georg Brandl116aa622007-08-15 14:28:22 +000033
34* If either argument is a complex number, the other is converted to complex;
35
36* otherwise, if either argument is a floating point number, the other is
37 converted to floating point;
38
Georg Brandl96593ed2007-09-07 14:15:41 +000039* otherwise, both must be integers and no conversion is necessary.
Georg Brandl116aa622007-08-15 14:28:22 +000040
Raymond Hettingeraa7886d2014-05-26 22:20:37 -070041Some additional rules apply for certain operators (e.g., a string as a left
42argument to the '%' operator). Extensions must define their own conversion
43behavior.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
46.. _atoms:
47
48Atoms
49=====
50
Georg Brandl96593ed2007-09-07 14:15:41 +000051.. index:: atom
Georg Brandl116aa622007-08-15 14:28:22 +000052
53Atoms are the most basic elements of expressions. The simplest atoms are
Georg Brandl96593ed2007-09-07 14:15:41 +000054identifiers or literals. Forms enclosed in parentheses, brackets or braces are
55also categorized syntactically as atoms. The syntax for atoms is:
Georg Brandl116aa622007-08-15 14:28:22 +000056
57.. productionlist::
58 atom: `identifier` | `literal` | `enclosure`
Georg Brandl96593ed2007-09-07 14:15:41 +000059 enclosure: `parenth_form` | `list_display` | `dict_display` | `set_display`
60 : | `generator_expression` | `yield_atom`
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
63.. _atom-identifiers:
64
65Identifiers (Names)
66-------------------
67
Georg Brandl96593ed2007-09-07 14:15:41 +000068.. index:: name, identifier
Georg Brandl116aa622007-08-15 14:28:22 +000069
70An identifier occurring as an atom is a name. See section :ref:`identifiers`
71for lexical definition and section :ref:`naming` for documentation of naming and
72binding.
73
74.. index:: exception: NameError
75
76When the name is bound to an object, evaluation of the atom yields that object.
77When a name is not bound, an attempt to evaluate it raises a :exc:`NameError`
78exception.
79
80.. index::
81 pair: name; mangling
82 pair: private; names
83
84**Private name mangling:** When an identifier that textually occurs in a class
85definition begins with two or more underscore characters and does not end in two
86or more underscores, it is considered a :dfn:`private name` of that class.
87Private names are transformed to a longer form before code is generated for
Georg Brandldec3b3f2013-04-14 10:13:42 +020088them. The transformation inserts the class name, with leading underscores
89removed and a single underscore inserted, in front of the name. For example,
90the identifier ``__spam`` occurring in a class named ``Ham`` will be transformed
91to ``_Ham__spam``. This transformation is independent of the syntactical
92context in which the identifier is used. If the transformed name is extremely
93long (longer than 255 characters), implementation defined truncation may happen.
94If the class name consists only of underscores, no transformation is done.
Georg Brandl116aa622007-08-15 14:28:22 +000095
Georg Brandl116aa622007-08-15 14:28:22 +000096
97.. _atom-literals:
98
99Literals
100--------
101
102.. index:: single: literal
103
Georg Brandl96593ed2007-09-07 14:15:41 +0000104Python supports string and bytes literals and various numeric literals:
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106.. productionlist::
Georg Brandl96593ed2007-09-07 14:15:41 +0000107 literal: `stringliteral` | `bytesliteral`
108 : | `integer` | `floatnumber` | `imagnumber`
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Georg Brandl96593ed2007-09-07 14:15:41 +0000110Evaluation of a literal yields an object of the given type (string, bytes,
111integer, floating point number, complex number) with the given value. The value
112may be approximated in the case of floating point and imaginary (complex)
Georg Brandl116aa622007-08-15 14:28:22 +0000113literals. See section :ref:`literals` for details.
114
115.. index::
116 triple: immutable; data; type
117 pair: immutable; object
118
Terry Jan Reedyead1de22012-02-17 19:56:58 -0500119All literals correspond to immutable data types, and hence the object's identity
120is less important than its value. Multiple evaluations of literals with the
121same value (either the same occurrence in the program text or a different
122occurrence) may obtain the same object or a different object with the same
123value.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. _parenthesized:
127
128Parenthesized forms
129-------------------
130
131.. index:: single: parenthesized form
132
133A parenthesized form is an optional expression list enclosed in parentheses:
134
135.. productionlist::
Martin Panter0c0da482016-06-12 01:46:50 +0000136 parenth_form: "(" [`starred_expression`] ")"
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138A parenthesized expression list yields whatever that expression list yields: if
139the list contains at least one comma, it yields a tuple; otherwise, it yields
140the single expression that makes up the expression list.
141
142.. index:: pair: empty; tuple
143
144An empty pair of parentheses yields an empty tuple object. Since tuples are
145immutable, the rules for literals apply (i.e., two occurrences of the empty
146tuple may or may not yield the same object).
147
148.. index::
149 single: comma
150 pair: tuple; display
151
152Note that tuples are not formed by the parentheses, but rather by use of the
153comma operator. The exception is the empty tuple, for which parentheses *are*
154required --- allowing unparenthesized "nothing" in expressions would cause
155ambiguities and allow common typos to pass uncaught.
156
157
Georg Brandl96593ed2007-09-07 14:15:41 +0000158.. _comprehensions:
159
160Displays for lists, sets and dictionaries
161-----------------------------------------
162
163For constructing a list, a set or a dictionary Python provides special syntax
164called "displays", each of them in two flavors:
165
166* either the container contents are listed explicitly, or
167
168* they are computed via a set of looping and filtering instructions, called a
169 :dfn:`comprehension`.
170
171Common syntax elements for comprehensions are:
172
173.. productionlist::
174 comprehension: `expression` `comp_for`
Yury Selivanov03660042016-12-15 17:36:05 -0500175 comp_for: [ASYNC] "for" `target_list` "in" `or_test` [`comp_iter`]
Georg Brandl96593ed2007-09-07 14:15:41 +0000176 comp_iter: `comp_for` | `comp_if`
177 comp_if: "if" `expression_nocond` [`comp_iter`]
178
179The comprehension consists of a single expression followed by at least one
180:keyword:`for` clause and zero or more :keyword:`for` or :keyword:`if` clauses.
181In this case, the elements of the new container are those that would be produced
182by considering each of the :keyword:`for` or :keyword:`if` clauses a block,
183nesting from left to right, and evaluating the expression to produce an element
184each time the innermost block is reached.
185
Georg Brandl02c30562007-09-07 17:52:53 +0000186Note that the comprehension is executed in a separate scope, so names assigned
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700187to in the target list don't "leak" into the enclosing scope.
Georg Brandl02c30562007-09-07 17:52:53 +0000188
Yury Selivanov03660042016-12-15 17:36:05 -0500189Since Python 3.6, in an :keyword:`async def` function, an :keyword:`async for`
190clause may be used to iterate over a :term:`asynchronous iterator`.
191A comprehension in an :keyword:`async def` function may consist of either a
192:keyword:`for` or :keyword:`async for` clause following the leading
Jim Fasarakis-Hilliard132ac382017-02-24 22:32:54 +0200193expression, may contain additional :keyword:`for` or :keyword:`async for`
Yury Selivanov03660042016-12-15 17:36:05 -0500194clauses, and may also use :keyword:`await` expressions.
195If a comprehension contains either :keyword:`async for` clauses
196or :keyword:`await` expressions it is called an
197:dfn:`asynchronous comprehension`. An asynchronous comprehension may
198suspend the execution of the coroutine function in which it appears.
199See also :pep:`530`.
Georg Brandl96593ed2007-09-07 14:15:41 +0000200
Georg Brandl116aa622007-08-15 14:28:22 +0000201.. _lists:
202
203List displays
204-------------
205
206.. index::
207 pair: list; display
208 pair: list; comprehensions
Georg Brandl96593ed2007-09-07 14:15:41 +0000209 pair: empty; list
210 object: list
Georg Brandl116aa622007-08-15 14:28:22 +0000211
212A list display is a possibly empty series of expressions enclosed in square
213brackets:
214
215.. productionlist::
Martin Panter0c0da482016-06-12 01:46:50 +0000216 list_display: "[" [`starred_list` | `comprehension`] "]"
Georg Brandl116aa622007-08-15 14:28:22 +0000217
Georg Brandl96593ed2007-09-07 14:15:41 +0000218A list display yields a new list object, the contents being specified by either
219a list of expressions or a comprehension. When a comma-separated list of
220expressions is supplied, its elements are evaluated from left to right and
221placed into the list object in that order. When a comprehension is supplied,
222the list is constructed from the elements resulting from the comprehension.
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224
Georg Brandl96593ed2007-09-07 14:15:41 +0000225.. _set:
Georg Brandl116aa622007-08-15 14:28:22 +0000226
Georg Brandl96593ed2007-09-07 14:15:41 +0000227Set displays
228------------
Georg Brandl116aa622007-08-15 14:28:22 +0000229
Georg Brandl96593ed2007-09-07 14:15:41 +0000230.. index:: pair: set; display
231 object: set
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Georg Brandl96593ed2007-09-07 14:15:41 +0000233A set display is denoted by curly braces and distinguishable from dictionary
234displays by the lack of colons separating keys and values:
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236.. productionlist::
Martin Panter0c0da482016-06-12 01:46:50 +0000237 set_display: "{" (`starred_list` | `comprehension`) "}"
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Georg Brandl96593ed2007-09-07 14:15:41 +0000239A set display yields a new mutable set object, the contents being specified by
240either a sequence of expressions or a comprehension. When a comma-separated
241list of expressions is supplied, its elements are evaluated from left to right
242and added to the set object. When a comprehension is supplied, the set is
243constructed from the elements resulting from the comprehension.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Georg Brandl528cdb12008-09-21 07:09:51 +0000245An empty set cannot be constructed with ``{}``; this literal constructs an empty
246dictionary.
Christian Heimes78644762008-03-04 23:39:23 +0000247
248
Georg Brandl116aa622007-08-15 14:28:22 +0000249.. _dict:
250
251Dictionary displays
252-------------------
253
254.. index:: pair: dictionary; display
Georg Brandl96593ed2007-09-07 14:15:41 +0000255 key, datum, key/datum pair
256 object: dictionary
Georg Brandl116aa622007-08-15 14:28:22 +0000257
258A dictionary display is a possibly empty series of key/datum pairs enclosed in
259curly braces:
260
261.. productionlist::
Georg Brandl96593ed2007-09-07 14:15:41 +0000262 dict_display: "{" [`key_datum_list` | `dict_comprehension`] "}"
Georg Brandl116aa622007-08-15 14:28:22 +0000263 key_datum_list: `key_datum` ("," `key_datum`)* [","]
Martin Panter0c0da482016-06-12 01:46:50 +0000264 key_datum: `expression` ":" `expression` | "**" `or_expr`
Georg Brandl96593ed2007-09-07 14:15:41 +0000265 dict_comprehension: `expression` ":" `expression` `comp_for`
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267A dictionary display yields a new dictionary object.
268
Georg Brandl96593ed2007-09-07 14:15:41 +0000269If a comma-separated sequence of key/datum pairs is given, they are evaluated
270from left to right to define the entries of the dictionary: each key object is
271used as a key into the dictionary to store the corresponding datum. This means
272that you can specify the same key multiple times in the key/datum list, and the
273final dictionary's value for that key will be the last one given.
274
Martin Panter0c0da482016-06-12 01:46:50 +0000275.. index:: unpacking; dictionary, **; in dictionary displays
276
277A double asterisk ``**`` denotes :dfn:`dictionary unpacking`.
278Its operand must be a :term:`mapping`. Each mapping item is added
279to the new dictionary. Later values replace values already set by
280earlier key/datum pairs and earlier dictionary unpackings.
281
282.. versionadded:: 3.5
283 Unpacking into dictionary displays, originally proposed by :pep:`448`.
284
Georg Brandl96593ed2007-09-07 14:15:41 +0000285A dict comprehension, in contrast to list and set comprehensions, needs two
286expressions separated with a colon followed by the usual "for" and "if" clauses.
287When the comprehension is run, the resulting key and value elements are inserted
288in the new dictionary in the order they are produced.
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290.. index:: pair: immutable; object
Georg Brandl96593ed2007-09-07 14:15:41 +0000291 hashable
Georg Brandl116aa622007-08-15 14:28:22 +0000292
293Restrictions on the types of the key values are listed earlier in section
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000294:ref:`types`. (To summarize, the key type should be :term:`hashable`, which excludes
Georg Brandl116aa622007-08-15 14:28:22 +0000295all mutable objects.) Clashes between duplicate keys are not detected; the last
296datum (textually rightmost in the display) stored for a given key value
297prevails.
298
299
Georg Brandl96593ed2007-09-07 14:15:41 +0000300.. _genexpr:
301
302Generator expressions
303---------------------
304
305.. index:: pair: generator; expression
306 object: generator
307
308A generator expression is a compact generator notation in parentheses:
309
310.. productionlist::
311 generator_expression: "(" `expression` `comp_for` ")"
312
313A generator expression yields a new generator object. Its syntax is the same as
314for comprehensions, except that it is enclosed in parentheses instead of
315brackets or curly braces.
316
317Variables used in the generator expression are evaluated lazily when the
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700318:meth:`~generator.__next__` method is called for the generator object (in the same
Ezio Melotti7fa82222012-10-12 13:42:08 +0300319fashion as normal generators). However, the leftmost :keyword:`for` clause is
320immediately evaluated, so that an error produced by it can be seen before any
321other possible error in the code that handles the generator expression.
322Subsequent :keyword:`for` clauses cannot be evaluated immediately since they
323may depend on the previous :keyword:`for` loop. For example: ``(x*y for x in
324range(10) for y in bar(x))``.
Georg Brandl96593ed2007-09-07 14:15:41 +0000325
326The parentheses can be omitted on calls with only one argument. See section
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700327:ref:`calls` for details.
Georg Brandl96593ed2007-09-07 14:15:41 +0000328
Yury Selivanovb8ab9d32017-10-06 02:58:28 -0400329If a generator expression contains either :keyword:`async for`
330clauses or :keyword:`await` expressions it is called an
331:dfn:`asynchronous generator expression`. An asynchronous generator
332expression returns a new asynchronous generator object,
333which is an asynchronous iterator (see :ref:`async-iterators`).
334
335.. versionchanged:: 3.7
336 Prior to Python 3.7, asynchronous generator expressions could
337 only appear in :keyword:`async def` coroutines. Starting
338 with 3.7, any function can use asynchronous generator expressions.
Georg Brandl96593ed2007-09-07 14:15:41 +0000339
Georg Brandl116aa622007-08-15 14:28:22 +0000340.. _yieldexpr:
341
342Yield expressions
343-----------------
344
345.. index::
346 keyword: yield
347 pair: yield; expression
348 pair: generator; function
349
350.. productionlist::
351 yield_atom: "(" `yield_expression` ")"
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000352 yield_expression: "yield" [`expression_list` | "from" `expression`]
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Yury Selivanov03660042016-12-15 17:36:05 -0500354The yield expression is used when defining a :term:`generator` function
355or an :term:`asynchronous generator` function and
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500356thus can only be used in the body of a function definition. Using a yield
Yury Selivanov03660042016-12-15 17:36:05 -0500357expression in a function's body causes that function to be a generator,
358and using it in an :keyword:`async def` function's body causes that
359coroutine function to be an asynchronous generator. For example::
360
361 def gen(): # defines a generator function
362 yield 123
363
364 async def agen(): # defines an asynchronous generator function (PEP 525)
365 yield 123
366
367Generator functions are described below, while asynchronous generator
368functions are described separately in section
369:ref:`asynchronous-generator-functions`.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371When a generator function is called, it returns an iterator known as a
Guido van Rossumd0150ad2015-05-05 12:02:01 -0700372generator. That generator then controls the execution of the generator function.
Georg Brandl116aa622007-08-15 14:28:22 +0000373The execution starts when one of the generator's methods is called. At that
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500374time, the execution proceeds to the first yield expression, where it is
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700375suspended again, returning the value of :token:`expression_list` to the generator's
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500376caller. By suspended, we mean that all local state is retained, including the
Ethan Furman2f825af2015-01-14 22:25:27 -0800377current bindings of local variables, the instruction pointer, the internal
378evaluation stack, and the state of any exception handling. When the execution
379is resumed by calling one of the
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500380generator's methods, the function can proceed exactly as if the yield expression
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700381were just another external call. The value of the yield expression after
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500382resuming depends on the method which resumed the execution. If
383:meth:`~generator.__next__` is used (typically via either a :keyword:`for` or
384the :func:`next` builtin) then the result is :const:`None`. Otherwise, if
385:meth:`~generator.send` is used, then the result will be the value passed in to
386that method.
Georg Brandl116aa622007-08-15 14:28:22 +0000387
388.. index:: single: coroutine
389
390All of this makes generator functions quite similar to coroutines; they yield
391multiple times, they have more than one entry point and their execution can be
392suspended. The only difference is that a generator function cannot control
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700393where the execution should continue after it yields; the control is always
Georg Brandl6faee4e2010-09-21 14:48:28 +0000394transferred to the generator's caller.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Ethan Furman2f825af2015-01-14 22:25:27 -0800396Yield expressions are allowed anywhere in a :keyword:`try` construct. If the
397generator is not resumed before it is
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500398finalized (by reaching a zero reference count or by being garbage collected),
399the generator-iterator's :meth:`~generator.close` method will be called,
400allowing any pending :keyword:`finally` clauses to execute.
Georg Brandl02c30562007-09-07 17:52:53 +0000401
Nick Coghlan0ed80192012-01-14 14:43:24 +1000402When ``yield from <expr>`` is used, it treats the supplied expression as
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000403a subiterator. All values produced by that subiterator are passed directly
404to the caller of the current generator's methods. Any values passed in with
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +0300405:meth:`~generator.send` and any exceptions passed in with
406:meth:`~generator.throw` are passed to the underlying iterator if it has the
407appropriate methods. If this is not the case, then :meth:`~generator.send`
408will raise :exc:`AttributeError` or :exc:`TypeError`, while
409:meth:`~generator.throw` will just raise the passed in exception immediately.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000410
411When the underlying iterator is complete, the :attr:`~StopIteration.value`
412attribute of the raised :exc:`StopIteration` instance becomes the value of
413the yield expression. It can be either set explicitly when raising
414:exc:`StopIteration`, or automatically when the sub-iterator is a generator
415(by returning a value from the sub-generator).
416
Nick Coghlan0ed80192012-01-14 14:43:24 +1000417 .. versionchanged:: 3.3
Martin Panterd21e0b52015-10-10 10:36:22 +0000418 Added ``yield from <expr>`` to delegate control flow to a subiterator.
Nick Coghlan0ed80192012-01-14 14:43:24 +1000419
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500420The parentheses may be omitted when the yield expression is the sole expression
421on the right hand side of an assignment statement.
422
423.. seealso::
424
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300425 :pep:`255` - Simple Generators
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500426 The proposal for adding generators and the :keyword:`yield` statement to Python.
427
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300428 :pep:`342` - Coroutines via Enhanced Generators
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500429 The proposal to enhance the API and syntax of generators, making them
430 usable as simple coroutines.
431
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300432 :pep:`380` - Syntax for Delegating to a Subgenerator
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500433 The proposal to introduce the :token:`yield_from` syntax, making delegation
434 to sub-generators easy.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000435
Georg Brandl116aa622007-08-15 14:28:22 +0000436.. index:: object: generator
Yury Selivanov66f88282015-06-24 11:04:15 -0400437.. _generator-methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000438
R David Murray2c1d1d62012-08-17 20:48:59 -0400439Generator-iterator methods
440^^^^^^^^^^^^^^^^^^^^^^^^^^
441
442This subsection describes the methods of a generator iterator. They can
443be used to control the execution of a generator function.
444
445Note that calling any of the generator methods below when the generator
446is already executing raises a :exc:`ValueError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448.. index:: exception: StopIteration
449
450
Georg Brandl96593ed2007-09-07 14:15:41 +0000451.. method:: generator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000452
Georg Brandl96593ed2007-09-07 14:15:41 +0000453 Starts the execution of a generator function or resumes it at the last
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500454 executed yield expression. When a generator function is resumed with a
455 :meth:`~generator.__next__` method, the current yield expression always
456 evaluates to :const:`None`. The execution then continues to the next yield
457 expression, where the generator is suspended again, and the value of the
Serhiy Storchaka848c8b22014-09-05 23:27:36 +0300458 :token:`expression_list` is returned to :meth:`__next__`'s caller. If the
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500459 generator exits without yielding another value, a :exc:`StopIteration`
Georg Brandl96593ed2007-09-07 14:15:41 +0000460 exception is raised.
461
462 This method is normally called implicitly, e.g. by a :keyword:`for` loop, or
463 by the built-in :func:`next` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000464
465
466.. method:: generator.send(value)
467
468 Resumes the execution and "sends" a value into the generator function. The
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500469 *value* argument becomes the result of the current yield expression. The
470 :meth:`send` method returns the next value yielded by the generator, or
471 raises :exc:`StopIteration` if the generator exits without yielding another
472 value. When :meth:`send` is called to start the generator, it must be called
473 with :const:`None` as the argument, because there is no yield expression that
474 could receive the value.
Georg Brandl116aa622007-08-15 14:28:22 +0000475
476
477.. method:: generator.throw(type[, value[, traceback]])
478
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700479 Raises an exception of type ``type`` at the point where the generator was paused,
Georg Brandl116aa622007-08-15 14:28:22 +0000480 and returns the next value yielded by the generator function. If the generator
481 exits without yielding another value, a :exc:`StopIteration` exception is
482 raised. If the generator function does not catch the passed-in exception, or
483 raises a different exception, then that exception propagates to the caller.
484
485.. index:: exception: GeneratorExit
486
487
488.. method:: generator.close()
489
490 Raises a :exc:`GeneratorExit` at the point where the generator function was
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400491 paused. If the generator function then exits gracefully, is already closed,
492 or raises :exc:`GeneratorExit` (by not catching the exception), close
493 returns to its caller. If the generator yields a value, a
494 :exc:`RuntimeError` is raised. If the generator raises any other exception,
495 it is propagated to the caller. :meth:`close` does nothing if the generator
496 has already exited due to an exception or normal exit.
Georg Brandl116aa622007-08-15 14:28:22 +0000497
Chris Jerdonek2654b862012-12-23 15:31:57 -0800498.. index:: single: yield; examples
499
500Examples
501^^^^^^^^
502
Georg Brandl116aa622007-08-15 14:28:22 +0000503Here is a simple example that demonstrates the behavior of generators and
504generator functions::
505
506 >>> def echo(value=None):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000507 ... print("Execution starts when 'next()' is called for the first time.")
Georg Brandl116aa622007-08-15 14:28:22 +0000508 ... try:
509 ... while True:
510 ... try:
511 ... value = (yield value)
Georg Brandlfe800a32009-08-03 17:50:20 +0000512 ... except Exception as e:
Georg Brandl116aa622007-08-15 14:28:22 +0000513 ... value = e
514 ... finally:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000515 ... print("Don't forget to clean up when 'close()' is called.")
Georg Brandl116aa622007-08-15 14:28:22 +0000516 ...
517 >>> generator = echo(1)
Georg Brandl96593ed2007-09-07 14:15:41 +0000518 >>> print(next(generator))
Georg Brandl116aa622007-08-15 14:28:22 +0000519 Execution starts when 'next()' is called for the first time.
520 1
Georg Brandl96593ed2007-09-07 14:15:41 +0000521 >>> print(next(generator))
Georg Brandl116aa622007-08-15 14:28:22 +0000522 None
Georg Brandl6911e3c2007-09-04 07:15:32 +0000523 >>> print(generator.send(2))
Georg Brandl116aa622007-08-15 14:28:22 +0000524 2
525 >>> generator.throw(TypeError, "spam")
526 TypeError('spam',)
527 >>> generator.close()
528 Don't forget to clean up when 'close()' is called.
529
Chris Jerdonek2654b862012-12-23 15:31:57 -0800530For examples using ``yield from``, see :ref:`pep-380` in "What's New in
531Python."
532
Yury Selivanov03660042016-12-15 17:36:05 -0500533.. _asynchronous-generator-functions:
534
535Asynchronous generator functions
536^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
537
538The presence of a yield expression in a function or method defined using
539:keyword:`async def` further defines the function as a
540:term:`asynchronous generator` function.
541
542When an asynchronous generator function is called, it returns an
543asynchronous iterator known as an asynchronous generator object.
544That object then controls the execution of the generator function.
545An asynchronous generator object is typically used in an
546:keyword:`async for` statement in a coroutine function analogously to
547how a generator object would be used in a :keyword:`for` statement.
548
549Calling one of the asynchronous generator's methods returns an
550:term:`awaitable` object, and the execution starts when this object
551is awaited on. At that time, the execution proceeds to the first yield
552expression, where it is suspended again, returning the value of
553:token:`expression_list` to the awaiting coroutine. As with a generator,
554suspension means that all local state is retained, including the
555current bindings of local variables, the instruction pointer, the internal
556evaluation stack, and the state of any exception handling. When the execution
557is resumed by awaiting on the next object returned by the asynchronous
558generator's methods, the function can proceed exactly as if the yield
559expression were just another external call. The value of the yield expression
560after resuming depends on the method which resumed the execution. If
561:meth:`~agen.__anext__` is used then the result is :const:`None`. Otherwise, if
562:meth:`~agen.asend` is used, then the result will be the value passed in to
563that method.
564
565In an asynchronous generator function, yield expressions are allowed anywhere
566in a :keyword:`try` construct. However, if an asynchronous generator is not
567resumed before it is finalized (by reaching a zero reference count or by
568being garbage collected), then a yield expression within a :keyword:`try`
569construct could result in a failure to execute pending :keyword:`finally`
570clauses. In this case, it is the responsibility of the event loop or
571scheduler running the asynchronous generator to call the asynchronous
572generator-iterator's :meth:`~agen.aclose` method and run the resulting
573coroutine object, thus allowing any pending :keyword:`finally` clauses
574to execute.
575
576To take care of finalization, an event loop should define
577a *finalizer* function which takes an asynchronous generator-iterator
578and presumably calls :meth:`~agen.aclose` and executes the coroutine.
579This *finalizer* may be registered by calling :func:`sys.set_asyncgen_hooks`.
580When first iterated over, an asynchronous generator-iterator will store the
581registered *finalizer* to be called upon finalization. For a reference example
582of a *finalizer* method see the implementation of
583``asyncio.Loop.shutdown_asyncgens`` in :source:`Lib/asyncio/base_events.py`.
584
585The expression ``yield from <expr>`` is a syntax error when used in an
586asynchronous generator function.
587
588.. index:: object: asynchronous-generator
589.. _asynchronous-generator-methods:
590
591Asynchronous generator-iterator methods
592^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
593
594This subsection describes the methods of an asynchronous generator iterator,
595which are used to control the execution of a generator function.
596
597
598.. index:: exception: StopAsyncIteration
599
600.. coroutinemethod:: agen.__anext__()
601
602 Returns an awaitable which when run starts to execute the asynchronous
603 generator or resumes it at the last executed yield expression. When an
604 asynchronous generator function is resumed with a :meth:`~agen.__anext__`
605 method, the current yield expression always evaluates to :const:`None` in
606 the returned awaitable, which when run will continue to the next yield
607 expression. The value of the :token:`expression_list` of the yield
608 expression is the value of the :exc:`StopIteration` exception raised by
609 the completing coroutine. If the asynchronous generator exits without
610 yielding another value, the awaitable instead raises an
611 :exc:`StopAsyncIteration` exception, signalling that the asynchronous
612 iteration has completed.
613
614 This method is normally called implicitly by a :keyword:`async for` loop.
615
616
617.. coroutinemethod:: agen.asend(value)
618
619 Returns an awaitable which when run resumes the execution of the
620 asynchronous generator. As with the :meth:`~generator.send()` method for a
621 generator, this "sends" a value into the asynchronous generator function,
622 and the *value* argument becomes the result of the current yield expression.
623 The awaitable returned by the :meth:`asend` method will return the next
624 value yielded by the generator as the value of the raised
625 :exc:`StopIteration`, or raises :exc:`StopAsyncIteration` if the
626 asynchronous generator exits without yielding another value. When
627 :meth:`asend` is called to start the asynchronous
628 generator, it must be called with :const:`None` as the argument,
629 because there is no yield expression that could receive the value.
630
631
632.. coroutinemethod:: agen.athrow(type[, value[, traceback]])
633
634 Returns an awaitable that raises an exception of type ``type`` at the point
635 where the asynchronous generator was paused, and returns the next value
636 yielded by the generator function as the value of the raised
637 :exc:`StopIteration` exception. If the asynchronous generator exits
638 without yielding another value, an :exc:`StopAsyncIteration` exception is
639 raised by the awaitable.
640 If the generator function does not catch the passed-in exception, or
delirious-lettuce3378b202017-05-19 14:37:57 -0600641 raises a different exception, then when the awaitable is run that exception
Yury Selivanov03660042016-12-15 17:36:05 -0500642 propagates to the caller of the awaitable.
643
644.. index:: exception: GeneratorExit
645
646
647.. coroutinemethod:: agen.aclose()
648
649 Returns an awaitable that when run will throw a :exc:`GeneratorExit` into
650 the asynchronous generator function at the point where it was paused.
651 If the asynchronous generator function then exits gracefully, is already
652 closed, or raises :exc:`GeneratorExit` (by not catching the exception),
653 then the returned awaitable will raise a :exc:`StopIteration` exception.
654 Any further awaitables returned by subsequent calls to the asynchronous
655 generator will raise a :exc:`StopAsyncIteration` exception. If the
656 asynchronous generator yields a value, a :exc:`RuntimeError` is raised
657 by the awaitable. If the asynchronous generator raises any other exception,
658 it is propagated to the caller of the awaitable. If the asynchronous
659 generator has already exited due to an exception or normal exit, then
660 further calls to :meth:`aclose` will return an awaitable that does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000661
Georg Brandl116aa622007-08-15 14:28:22 +0000662.. _primaries:
663
664Primaries
665=========
666
667.. index:: single: primary
668
669Primaries represent the most tightly bound operations of the language. Their
670syntax is:
671
672.. productionlist::
673 primary: `atom` | `attributeref` | `subscription` | `slicing` | `call`
674
675
676.. _attribute-references:
677
678Attribute references
679--------------------
680
681.. index:: pair: attribute; reference
682
683An attribute reference is a primary followed by a period and a name:
684
685.. productionlist::
686 attributeref: `primary` "." `identifier`
687
688.. index::
689 exception: AttributeError
690 object: module
691 object: list
692
693The primary must evaluate to an object of a type that supports attribute
Georg Brandl96593ed2007-09-07 14:15:41 +0000694references, which most objects do. This object is then asked to produce the
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700695attribute whose name is the identifier. This production can be customized by
Zachary Ware2f78b842014-06-03 09:32:40 -0500696overriding the :meth:`__getattr__` method. If this attribute is not available,
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700697the exception :exc:`AttributeError` is raised. Otherwise, the type and value of
698the object produced is determined by the object. Multiple evaluations of the
699same attribute reference may yield different objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000700
701
702.. _subscriptions:
703
704Subscriptions
705-------------
706
707.. index:: single: subscription
708
709.. index::
710 object: sequence
711 object: mapping
712 object: string
713 object: tuple
714 object: list
715 object: dictionary
716 pair: sequence; item
717
718A subscription selects an item of a sequence (string, tuple or list) or mapping
719(dictionary) object:
720
721.. productionlist::
722 subscription: `primary` "[" `expression_list` "]"
723
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700724The primary must evaluate to an object that supports subscription (lists or
725dictionaries for example). User-defined objects can support subscription by
726defining a :meth:`__getitem__` method.
Georg Brandl96593ed2007-09-07 14:15:41 +0000727
728For built-in objects, there are two types of objects that support subscription:
Georg Brandl116aa622007-08-15 14:28:22 +0000729
730If the primary is a mapping, the expression list must evaluate to an object
731whose value is one of the keys of the mapping, and the subscription selects the
732value in the mapping that corresponds to that key. (The expression list is a
733tuple except if it has exactly one item.)
734
Raymond Hettingerf77c1d62010-09-15 00:09:26 +0000735If the primary is a sequence, the expression (list) must evaluate to an integer
736or a slice (as discussed in the following section).
737
738The formal syntax makes no special provision for negative indices in
739sequences; however, built-in sequences all provide a :meth:`__getitem__`
740method that interprets negative indices by adding the length of the sequence
741to the index (so that ``x[-1]`` selects the last item of ``x``). The
742resulting value must be a nonnegative integer less than the number of items in
743the sequence, and the subscription selects the item whose index is that value
744(counting from zero). Since the support for negative indices and slicing
745occurs in the object's :meth:`__getitem__` method, subclasses overriding
746this method will need to explicitly add that support.
Georg Brandl116aa622007-08-15 14:28:22 +0000747
748.. index::
749 single: character
750 pair: string; item
751
752A string's items are characters. A character is not a separate data type but a
753string of exactly one character.
754
755
756.. _slicings:
757
758Slicings
759--------
760
761.. index::
762 single: slicing
763 single: slice
764
765.. index::
766 object: sequence
767 object: string
768 object: tuple
769 object: list
770
771A slicing selects a range of items in a sequence object (e.g., a string, tuple
772or list). Slicings may be used as expressions or as targets in assignment or
773:keyword:`del` statements. The syntax for a slicing:
774
775.. productionlist::
Georg Brandl48310cd2009-01-03 21:18:54 +0000776 slicing: `primary` "[" `slice_list` "]"
Georg Brandl116aa622007-08-15 14:28:22 +0000777 slice_list: `slice_item` ("," `slice_item`)* [","]
Georg Brandlcb8ecb12007-09-04 06:35:14 +0000778 slice_item: `expression` | `proper_slice`
Thomas Wouters53de1902007-09-04 09:03:59 +0000779 proper_slice: [`lower_bound`] ":" [`upper_bound`] [ ":" [`stride`] ]
Georg Brandl116aa622007-08-15 14:28:22 +0000780 lower_bound: `expression`
781 upper_bound: `expression`
782 stride: `expression`
Georg Brandl116aa622007-08-15 14:28:22 +0000783
784There is ambiguity in the formal syntax here: anything that looks like an
785expression list also looks like a slice list, so any subscription can be
786interpreted as a slicing. Rather than further complicating the syntax, this is
787disambiguated by defining that in this case the interpretation as a subscription
788takes priority over the interpretation as a slicing (this is the case if the
Thomas Wouters53de1902007-09-04 09:03:59 +0000789slice list contains no proper slice).
Georg Brandl116aa622007-08-15 14:28:22 +0000790
791.. index::
792 single: start (slice object attribute)
793 single: stop (slice object attribute)
794 single: step (slice object attribute)
795
Georg Brandla4c8c472014-10-31 10:38:49 +0100796The semantics for a slicing are as follows. The primary is indexed (using the
797same :meth:`__getitem__` method as
Georg Brandl96593ed2007-09-07 14:15:41 +0000798normal subscription) with a key that is constructed from the slice list, as
799follows. If the slice list contains at least one comma, the key is a tuple
800containing the conversion of the slice items; otherwise, the conversion of the
801lone slice item is the key. The conversion of a slice item that is an
802expression is that expression. The conversion of a proper slice is a slice
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +0300803object (see section :ref:`types`) whose :attr:`~slice.start`,
804:attr:`~slice.stop` and :attr:`~slice.step` attributes are the values of the
805expressions given as lower bound, upper bound and stride, respectively,
806substituting ``None`` for missing expressions.
Georg Brandl116aa622007-08-15 14:28:22 +0000807
808
Chris Jerdonekb4309942012-12-25 14:54:44 -0800809.. index::
810 object: callable
811 single: call
812 single: argument; call semantics
813
Georg Brandl116aa622007-08-15 14:28:22 +0000814.. _calls:
815
816Calls
817-----
818
Chris Jerdonekb4309942012-12-25 14:54:44 -0800819A call calls a callable object (e.g., a :term:`function`) with a possibly empty
820series of :term:`arguments <argument>`:
Georg Brandl116aa622007-08-15 14:28:22 +0000821
822.. productionlist::
Georg Brandldc529c12008-09-21 17:03:29 +0000823 call: `primary` "(" [`argument_list` [","] | `comprehension`] ")"
Martin Panter0c0da482016-06-12 01:46:50 +0000824 argument_list: `positional_arguments` ["," `starred_and_keywords`]
825 : ["," `keywords_arguments`]
826 : | `starred_and_keywords` ["," `keywords_arguments`]
827 : | `keywords_arguments`
828 positional_arguments: ["*"] `expression` ("," ["*"] `expression`)*
829 starred_and_keywords: ("*" `expression` | `keyword_item`)
830 : ("," "*" `expression` | "," `keyword_item`)*
831 keywords_arguments: (`keyword_item` | "**" `expression`)
Martin Panter7106a512016-12-24 10:20:38 +0000832 : ("," `keyword_item` | "," "**" `expression`)*
Georg Brandl116aa622007-08-15 14:28:22 +0000833 keyword_item: `identifier` "=" `expression`
834
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700835An optional trailing comma may be present after the positional and keyword arguments
836but does not affect the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000837
Chris Jerdonekb4309942012-12-25 14:54:44 -0800838.. index::
839 single: parameter; call semantics
840
Georg Brandl116aa622007-08-15 14:28:22 +0000841The primary must evaluate to a callable object (user-defined functions, built-in
842functions, methods of built-in objects, class objects, methods of class
Georg Brandl96593ed2007-09-07 14:15:41 +0000843instances, and all objects having a :meth:`__call__` method are callable). All
844argument expressions are evaluated before the call is attempted. Please refer
Chris Jerdonekb4309942012-12-25 14:54:44 -0800845to section :ref:`function` for the syntax of formal :term:`parameter` lists.
Georg Brandl96593ed2007-09-07 14:15:41 +0000846
847.. XXX update with kwonly args PEP
Georg Brandl116aa622007-08-15 14:28:22 +0000848
849If keyword arguments are present, they are first converted to positional
850arguments, as follows. First, a list of unfilled slots is created for the
851formal parameters. If there are N positional arguments, they are placed in the
852first N slots. Next, for each keyword argument, the identifier is used to
853determine the corresponding slot (if the identifier is the same as the first
854formal parameter name, the first slot is used, and so on). If the slot is
855already filled, a :exc:`TypeError` exception is raised. Otherwise, the value of
856the argument is placed in the slot, filling it (even if the expression is
857``None``, it fills the slot). When all arguments have been processed, the slots
858that are still unfilled are filled with the corresponding default value from the
859function definition. (Default values are calculated, once, when the function is
860defined; thus, a mutable object such as a list or dictionary used as default
861value will be shared by all calls that don't specify an argument value for the
862corresponding slot; this should usually be avoided.) If there are any unfilled
863slots for which no default value is specified, a :exc:`TypeError` exception is
864raised. Otherwise, the list of filled slots is used as the argument list for
865the call.
866
Georg Brandl495f7b52009-10-27 15:28:25 +0000867.. impl-detail::
Georg Brandl48310cd2009-01-03 21:18:54 +0000868
Georg Brandl495f7b52009-10-27 15:28:25 +0000869 An implementation may provide built-in functions whose positional parameters
870 do not have names, even if they are 'named' for the purpose of documentation,
871 and which therefore cannot be supplied by keyword. In CPython, this is the
Georg Brandl60203b42010-10-06 10:11:56 +0000872 case for functions implemented in C that use :c:func:`PyArg_ParseTuple` to
Georg Brandl495f7b52009-10-27 15:28:25 +0000873 parse their arguments.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000874
Georg Brandl116aa622007-08-15 14:28:22 +0000875If there are more positional arguments than there are formal parameter slots, a
876:exc:`TypeError` exception is raised, unless a formal parameter using the syntax
877``*identifier`` is present; in this case, that formal parameter receives a tuple
878containing the excess positional arguments (or an empty tuple if there were no
879excess positional arguments).
880
881If any keyword argument does not correspond to a formal parameter name, a
882:exc:`TypeError` exception is raised, unless a formal parameter using the syntax
883``**identifier`` is present; in this case, that formal parameter receives a
884dictionary containing the excess keyword arguments (using the keywords as keys
885and the argument values as corresponding values), or a (new) empty dictionary if
886there were no excess keyword arguments.
887
Eli Bendersky7bd081c2011-07-30 07:05:16 +0300888.. index::
889 single: *; in function calls
Martin Panter0c0da482016-06-12 01:46:50 +0000890 single: unpacking; in function calls
Eli Bendersky7bd081c2011-07-30 07:05:16 +0300891
Georg Brandl116aa622007-08-15 14:28:22 +0000892If the syntax ``*expression`` appears in the function call, ``expression`` must
Martin Panter0c0da482016-06-12 01:46:50 +0000893evaluate to an :term:`iterable`. Elements from these iterables are
894treated as if they were additional positional arguments. For the call
895``f(x1, x2, *y, x3, x4)``, if *y* evaluates to a sequence *y1*, ..., *yM*,
896this is equivalent to a call with M+4 positional arguments *x1*, *x2*,
897*y1*, ..., *yM*, *x3*, *x4*.
Georg Brandl116aa622007-08-15 14:28:22 +0000898
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000899A consequence of this is that although the ``*expression`` syntax may appear
Martin Panter0c0da482016-06-12 01:46:50 +0000900*after* explicit keyword arguments, it is processed *before* the
901keyword arguments (and any ``**expression`` arguments -- see below). So::
Georg Brandl116aa622007-08-15 14:28:22 +0000902
903 >>> def f(a, b):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300904 ... print(a, b)
Georg Brandl116aa622007-08-15 14:28:22 +0000905 ...
906 >>> f(b=1, *(2,))
907 2 1
908 >>> f(a=1, *(2,))
909 Traceback (most recent call last):
UltimateCoder88569402017-05-03 22:16:45 +0530910 File "<stdin>", line 1, in <module>
Georg Brandl116aa622007-08-15 14:28:22 +0000911 TypeError: f() got multiple values for keyword argument 'a'
912 >>> f(1, *(2,))
913 1 2
914
915It is unusual for both keyword arguments and the ``*expression`` syntax to be
916used in the same call, so in practice this confusion does not arise.
917
Eli Bendersky7bd081c2011-07-30 07:05:16 +0300918.. index::
919 single: **; in function calls
920
Georg Brandl116aa622007-08-15 14:28:22 +0000921If the syntax ``**expression`` appears in the function call, ``expression`` must
Martin Panter0c0da482016-06-12 01:46:50 +0000922evaluate to a :term:`mapping`, the contents of which are treated as
923additional keyword arguments. If a keyword is already present
924(as an explicit keyword argument, or from another unpacking),
925a :exc:`TypeError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000926
927Formal parameters using the syntax ``*identifier`` or ``**identifier`` cannot be
928used as positional argument slots or as keyword argument names.
929
Martin Panter0c0da482016-06-12 01:46:50 +0000930.. versionchanged:: 3.5
931 Function calls accept any number of ``*`` and ``**`` unpackings,
932 positional arguments may follow iterable unpackings (``*``),
933 and keyword arguments may follow dictionary unpackings (``**``).
934 Originally proposed by :pep:`448`.
935
Georg Brandl116aa622007-08-15 14:28:22 +0000936A call always returns some value, possibly ``None``, unless it raises an
937exception. How this value is computed depends on the type of the callable
938object.
939
940If it is---
941
942a user-defined function:
943 .. index::
944 pair: function; call
945 triple: user-defined; function; call
946 object: user-defined function
947 object: function
948
949 The code block for the function is executed, passing it the argument list. The
950 first thing the code block will do is bind the formal parameters to the
951 arguments; this is described in section :ref:`function`. When the code block
952 executes a :keyword:`return` statement, this specifies the return value of the
953 function call.
954
955a built-in function or method:
956 .. index::
957 pair: function; call
958 pair: built-in function; call
959 pair: method; call
960 pair: built-in method; call
961 object: built-in method
962 object: built-in function
963 object: method
964 object: function
965
966 The result is up to the interpreter; see :ref:`built-in-funcs` for the
967 descriptions of built-in functions and methods.
968
969a class object:
970 .. index::
971 object: class
972 pair: class object; call
973
974 A new instance of that class is returned.
975
976a class instance method:
977 .. index::
978 object: class instance
979 object: instance
980 pair: class instance; call
981
982 The corresponding user-defined function is called, with an argument list that is
983 one longer than the argument list of the call: the instance becomes the first
984 argument.
985
986a class instance:
987 .. index::
988 pair: instance; call
989 single: __call__() (object method)
990
991 The class must define a :meth:`__call__` method; the effect is then the same as
992 if that method was called.
993
994
Yury Selivanovf3e40fa2015-05-21 11:50:30 -0400995.. _await:
996
997Await expression
998================
999
1000Suspend the execution of :term:`coroutine` on an :term:`awaitable` object.
1001Can only be used inside a :term:`coroutine function`.
1002
1003.. productionlist::
Serhiy Storchakac7cc9852016-05-08 21:59:46 +03001004 await_expr: "await" `primary`
Yury Selivanovf3e40fa2015-05-21 11:50:30 -04001005
1006.. versionadded:: 3.5
1007
1008
Georg Brandl116aa622007-08-15 14:28:22 +00001009.. _power:
1010
1011The power operator
1012==================
1013
1014The power operator binds more tightly than unary operators on its left; it binds
1015less tightly than unary operators on its right. The syntax is:
1016
1017.. productionlist::
Serhiy Storchakac7cc9852016-05-08 21:59:46 +03001018 power: ( `await_expr` | `primary` ) ["**" `u_expr`]
Georg Brandl116aa622007-08-15 14:28:22 +00001019
1020Thus, in an unparenthesized sequence of power and unary operators, the operators
1021are evaluated from right to left (this does not constrain the evaluation order
Guido van Rossum04110fb2007-08-24 16:32:05 +00001022for the operands): ``-1**2`` results in ``-1``.
Georg Brandl116aa622007-08-15 14:28:22 +00001023
1024The power operator has the same semantics as the built-in :func:`pow` function,
1025when called with two arguments: it yields its left argument raised to the power
1026of its right argument. The numeric arguments are first converted to a common
Georg Brandl96593ed2007-09-07 14:15:41 +00001027type, and the result is of that type.
Georg Brandl116aa622007-08-15 14:28:22 +00001028
Georg Brandl96593ed2007-09-07 14:15:41 +00001029For int operands, the result has the same type as the operands unless the second
1030argument is negative; in that case, all arguments are converted to float and a
1031float result is delivered. For example, ``10**2`` returns ``100``, but
1032``10**-2`` returns ``0.01``.
Georg Brandl116aa622007-08-15 14:28:22 +00001033
1034Raising ``0.0`` to a negative power results in a :exc:`ZeroDivisionError`.
Christian Heimes072c0f12008-01-03 23:01:04 +00001035Raising a negative number to a fractional power results in a :class:`complex`
Christian Heimesfaf2f632008-01-06 16:59:19 +00001036number. (In earlier versions it raised a :exc:`ValueError`.)
Georg Brandl116aa622007-08-15 14:28:22 +00001037
1038
1039.. _unary:
1040
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001041Unary arithmetic and bitwise operations
1042=======================================
Georg Brandl116aa622007-08-15 14:28:22 +00001043
1044.. index::
1045 triple: unary; arithmetic; operation
Christian Heimesfaf2f632008-01-06 16:59:19 +00001046 triple: unary; bitwise; operation
Georg Brandl116aa622007-08-15 14:28:22 +00001047
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001048All unary arithmetic and bitwise operations have the same priority:
Georg Brandl116aa622007-08-15 14:28:22 +00001049
1050.. productionlist::
1051 u_expr: `power` | "-" `u_expr` | "+" `u_expr` | "~" `u_expr`
1052
1053.. index::
1054 single: negation
1055 single: minus
1056
1057The unary ``-`` (minus) operator yields the negation of its numeric argument.
1058
1059.. index:: single: plus
1060
1061The unary ``+`` (plus) operator yields its numeric argument unchanged.
1062
1063.. index:: single: inversion
1064
Christian Heimesfaf2f632008-01-06 16:59:19 +00001065
Georg Brandl95817b32008-05-11 14:30:18 +00001066The unary ``~`` (invert) operator yields the bitwise inversion of its integer
1067argument. The bitwise inversion of ``x`` is defined as ``-(x+1)``. It only
1068applies to integral numbers.
Georg Brandl116aa622007-08-15 14:28:22 +00001069
1070.. index:: exception: TypeError
1071
1072In all three cases, if the argument does not have the proper type, a
1073:exc:`TypeError` exception is raised.
1074
1075
1076.. _binary:
1077
1078Binary arithmetic operations
1079============================
1080
1081.. index:: triple: binary; arithmetic; operation
1082
1083The binary arithmetic operations have the conventional priority levels. Note
1084that some of these operations also apply to certain non-numeric types. Apart
1085from the power operator, there are only two levels, one for multiplicative
1086operators and one for additive operators:
1087
1088.. productionlist::
Benjamin Petersond51374e2014-04-09 23:55:56 -04001089 m_expr: `u_expr` | `m_expr` "*" `u_expr` | `m_expr` "@" `m_expr` |
1090 : `m_expr` "//" `u_expr`| `m_expr` "/" `u_expr` |
1091 : `m_expr` "%" `u_expr`
Georg Brandl116aa622007-08-15 14:28:22 +00001092 a_expr: `m_expr` | `a_expr` "+" `m_expr` | `a_expr` "-" `m_expr`
1093
1094.. index:: single: multiplication
1095
1096The ``*`` (multiplication) operator yields the product of its arguments. The
Georg Brandl96593ed2007-09-07 14:15:41 +00001097arguments must either both be numbers, or one argument must be an integer and
1098the other must be a sequence. In the former case, the numbers are converted to a
1099common type and then multiplied together. In the latter case, sequence
1100repetition is performed; a negative repetition factor yields an empty sequence.
Georg Brandl116aa622007-08-15 14:28:22 +00001101
Benjamin Petersond51374e2014-04-09 23:55:56 -04001102.. index:: single: matrix multiplication
1103
1104The ``@`` (at) operator is intended to be used for matrix multiplication. No
1105builtin Python types implement this operator.
1106
1107.. versionadded:: 3.5
1108
Georg Brandl116aa622007-08-15 14:28:22 +00001109.. index::
1110 exception: ZeroDivisionError
1111 single: division
1112
1113The ``/`` (division) and ``//`` (floor division) operators yield the quotient of
1114their arguments. The numeric arguments are first converted to a common type.
Georg Brandl0aaae262013-10-08 21:47:18 +02001115Division of integers yields a float, while floor division of integers results in an
Georg Brandl96593ed2007-09-07 14:15:41 +00001116integer; the result is that of mathematical division with the 'floor' function
1117applied to the result. Division by zero raises the :exc:`ZeroDivisionError`
1118exception.
Georg Brandl116aa622007-08-15 14:28:22 +00001119
1120.. index:: single: modulo
1121
1122The ``%`` (modulo) operator yields the remainder from the division of the first
1123argument by the second. The numeric arguments are first converted to a common
1124type. A zero right argument raises the :exc:`ZeroDivisionError` exception. The
1125arguments may be floating point numbers, e.g., ``3.14%0.7`` equals ``0.34``
1126(since ``3.14`` equals ``4*0.7 + 0.34``.) The modulo operator always yields a
1127result with the same sign as its second operand (or zero); the absolute value of
1128the result is strictly smaller than the absolute value of the second operand
1129[#]_.
1130
Georg Brandl96593ed2007-09-07 14:15:41 +00001131The floor division and modulo operators are connected by the following
1132identity: ``x == (x//y)*y + (x%y)``. Floor division and modulo are also
1133connected with the built-in function :func:`divmod`: ``divmod(x, y) == (x//y,
1134x%y)``. [#]_.
Georg Brandl116aa622007-08-15 14:28:22 +00001135
1136In addition to performing the modulo operation on numbers, the ``%`` operator is
Georg Brandl96593ed2007-09-07 14:15:41 +00001137also overloaded by string objects to perform old-style string formatting (also
1138known as interpolation). The syntax for string formatting is described in the
Georg Brandl4b491312007-08-31 09:22:56 +00001139Python Library Reference, section :ref:`old-string-formatting`.
Georg Brandl116aa622007-08-15 14:28:22 +00001140
1141The floor division operator, the modulo operator, and the :func:`divmod`
Georg Brandl96593ed2007-09-07 14:15:41 +00001142function are not defined for complex numbers. Instead, convert to a floating
1143point number using the :func:`abs` function if appropriate.
Georg Brandl116aa622007-08-15 14:28:22 +00001144
1145.. index:: single: addition
1146
Georg Brandl96593ed2007-09-07 14:15:41 +00001147The ``+`` (addition) operator yields the sum of its arguments. The arguments
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001148must either both be numbers or both be sequences of the same type. In the
1149former case, the numbers are converted to a common type and then added together.
1150In the latter case, the sequences are concatenated.
Georg Brandl116aa622007-08-15 14:28:22 +00001151
1152.. index:: single: subtraction
1153
1154The ``-`` (subtraction) operator yields the difference of its arguments. The
1155numeric arguments are first converted to a common type.
1156
1157
1158.. _shifting:
1159
1160Shifting operations
1161===================
1162
1163.. index:: pair: shifting; operation
1164
1165The shifting operations have lower priority than the arithmetic operations:
1166
1167.. productionlist::
1168 shift_expr: `a_expr` | `shift_expr` ( "<<" | ">>" ) `a_expr`
1169
Georg Brandl96593ed2007-09-07 14:15:41 +00001170These operators accept integers as arguments. They shift the first argument to
1171the left or right by the number of bits given by the second argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001172
1173.. index:: exception: ValueError
1174
Georg Brandl0aaae262013-10-08 21:47:18 +02001175A right shift by *n* bits is defined as floor division by ``pow(2,n)``. A left
1176shift by *n* bits is defined as multiplication with ``pow(2,n)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001177
1178
1179.. _bitwise:
1180
Christian Heimesfaf2f632008-01-06 16:59:19 +00001181Binary bitwise operations
1182=========================
Georg Brandl116aa622007-08-15 14:28:22 +00001183
Christian Heimesfaf2f632008-01-06 16:59:19 +00001184.. index:: triple: binary; bitwise; operation
Georg Brandl116aa622007-08-15 14:28:22 +00001185
1186Each of the three bitwise operations has a different priority level:
1187
1188.. productionlist::
1189 and_expr: `shift_expr` | `and_expr` "&" `shift_expr`
1190 xor_expr: `and_expr` | `xor_expr` "^" `and_expr`
1191 or_expr: `xor_expr` | `or_expr` "|" `xor_expr`
1192
Christian Heimesfaf2f632008-01-06 16:59:19 +00001193.. index:: pair: bitwise; and
Georg Brandl116aa622007-08-15 14:28:22 +00001194
Georg Brandl96593ed2007-09-07 14:15:41 +00001195The ``&`` operator yields the bitwise AND of its arguments, which must be
1196integers.
Georg Brandl116aa622007-08-15 14:28:22 +00001197
1198.. index::
Christian Heimesfaf2f632008-01-06 16:59:19 +00001199 pair: bitwise; xor
Georg Brandl116aa622007-08-15 14:28:22 +00001200 pair: exclusive; or
1201
1202The ``^`` operator yields the bitwise XOR (exclusive OR) of its arguments, which
Georg Brandl96593ed2007-09-07 14:15:41 +00001203must be integers.
Georg Brandl116aa622007-08-15 14:28:22 +00001204
1205.. index::
Christian Heimesfaf2f632008-01-06 16:59:19 +00001206 pair: bitwise; or
Georg Brandl116aa622007-08-15 14:28:22 +00001207 pair: inclusive; or
1208
1209The ``|`` operator yields the bitwise (inclusive) OR of its arguments, which
Georg Brandl96593ed2007-09-07 14:15:41 +00001210must be integers.
Georg Brandl116aa622007-08-15 14:28:22 +00001211
1212
1213.. _comparisons:
1214
1215Comparisons
1216===========
1217
1218.. index:: single: comparison
1219
1220.. index:: pair: C; language
1221
1222Unlike C, all comparison operations in Python have the same priority, which is
1223lower than that of any arithmetic, shifting or bitwise operation. Also unlike
1224C, expressions like ``a < b < c`` have the interpretation that is conventional
1225in mathematics:
1226
1227.. productionlist::
1228 comparison: `or_expr` ( `comp_operator` `or_expr` )*
1229 comp_operator: "<" | ">" | "==" | ">=" | "<=" | "!="
1230 : | "is" ["not"] | ["not"] "in"
1231
1232Comparisons yield boolean values: ``True`` or ``False``.
1233
1234.. index:: pair: chaining; comparisons
1235
1236Comparisons can be chained arbitrarily, e.g., ``x < y <= z`` is equivalent to
1237``x < y and y <= z``, except that ``y`` is evaluated only once (but in both
1238cases ``z`` is not evaluated at all when ``x < y`` is found to be false).
1239
Guido van Rossum04110fb2007-08-24 16:32:05 +00001240Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*, *op2*, ...,
1241*opN* are comparison operators, then ``a op1 b op2 c ... y opN z`` is equivalent
1242to ``a op1 b and b op2 c and ... y opN z``, except that each expression is
1243evaluated at most once.
Georg Brandl116aa622007-08-15 14:28:22 +00001244
Guido van Rossum04110fb2007-08-24 16:32:05 +00001245Note that ``a op1 b op2 c`` doesn't imply any kind of comparison between *a* and
Georg Brandl116aa622007-08-15 14:28:22 +00001246*c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not
1247pretty).
1248
Martin Panteraa0da862015-09-23 05:28:13 +00001249Value comparisons
1250-----------------
1251
Georg Brandl116aa622007-08-15 14:28:22 +00001252The operators ``<``, ``>``, ``==``, ``>=``, ``<=``, and ``!=`` compare the
Martin Panteraa0da862015-09-23 05:28:13 +00001253values of two objects. The objects do not need to have the same type.
Georg Brandl116aa622007-08-15 14:28:22 +00001254
Martin Panteraa0da862015-09-23 05:28:13 +00001255Chapter :ref:`objects` states that objects have a value (in addition to type
1256and identity). The value of an object is a rather abstract notion in Python:
1257For example, there is no canonical access method for an object's value. Also,
1258there is no requirement that the value of an object should be constructed in a
1259particular way, e.g. comprised of all its data attributes. Comparison operators
1260implement a particular notion of what the value of an object is. One can think
1261of them as defining the value of an object indirectly, by means of their
1262comparison implementation.
Georg Brandl116aa622007-08-15 14:28:22 +00001263
Martin Panteraa0da862015-09-23 05:28:13 +00001264Because all types are (direct or indirect) subtypes of :class:`object`, they
1265inherit the default comparison behavior from :class:`object`. Types can
1266customize their comparison behavior by implementing
1267:dfn:`rich comparison methods` like :meth:`__lt__`, described in
1268:ref:`customization`.
Georg Brandl116aa622007-08-15 14:28:22 +00001269
Martin Panteraa0da862015-09-23 05:28:13 +00001270The default behavior for equality comparison (``==`` and ``!=``) is based on
1271the identity of the objects. Hence, equality comparison of instances with the
1272same identity results in equality, and equality comparison of instances with
1273different identities results in inequality. A motivation for this default
1274behavior is the desire that all objects should be reflexive (i.e. ``x is y``
1275implies ``x == y``).
1276
1277A default order comparison (``<``, ``>``, ``<=``, and ``>=``) is not provided;
1278an attempt raises :exc:`TypeError`. A motivation for this default behavior is
1279the lack of a similar invariant as for equality.
1280
1281The behavior of the default equality comparison, that instances with different
1282identities are always unequal, may be in contrast to what types will need that
1283have a sensible definition of object value and value-based equality. Such
1284types will need to customize their comparison behavior, and in fact, a number
1285of built-in types have done that.
1286
1287The following list describes the comparison behavior of the most important
1288built-in types.
1289
1290* Numbers of built-in numeric types (:ref:`typesnumeric`) and of the standard
1291 library types :class:`fractions.Fraction` and :class:`decimal.Decimal` can be
1292 compared within and across their types, with the restriction that complex
1293 numbers do not support order comparison. Within the limits of the types
1294 involved, they compare mathematically (algorithmically) correct without loss
1295 of precision.
1296
1297 The not-a-number values :const:`float('NaN')` and :const:`Decimal('NaN')`
1298 are special. They are identical to themselves (``x is x`` is true) but
1299 are not equal to themselves (``x == x`` is false). Additionally,
1300 comparing any number to a not-a-number value
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001301 will return ``False``. For example, both ``3 < float('NaN')`` and
1302 ``float('NaN') < 3`` will return ``False``.
1303
Martin Panteraa0da862015-09-23 05:28:13 +00001304* Binary sequences (instances of :class:`bytes` or :class:`bytearray`) can be
1305 compared within and across their types. They compare lexicographically using
1306 the numeric values of their elements.
Georg Brandl4b491312007-08-31 09:22:56 +00001307
Martin Panteraa0da862015-09-23 05:28:13 +00001308* Strings (instances of :class:`str`) compare lexicographically using the
1309 numerical Unicode code points (the result of the built-in function
1310 :func:`ord`) of their characters. [#]_
Georg Brandl116aa622007-08-15 14:28:22 +00001311
Martin Panteraa0da862015-09-23 05:28:13 +00001312 Strings and binary sequences cannot be directly compared.
Georg Brandl116aa622007-08-15 14:28:22 +00001313
Martin Panteraa0da862015-09-23 05:28:13 +00001314* Sequences (instances of :class:`tuple`, :class:`list`, or :class:`range`) can
1315 be compared only within each of their types, with the restriction that ranges
1316 do not support order comparison. Equality comparison across these types
Jim Fasarakis-Hilliard132ac382017-02-24 22:32:54 +02001317 results in inequality, and ordering comparison across these types raises
Martin Panteraa0da862015-09-23 05:28:13 +00001318 :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +00001319
Martin Panteraa0da862015-09-23 05:28:13 +00001320 Sequences compare lexicographically using comparison of corresponding
1321 elements, whereby reflexivity of the elements is enforced.
Georg Brandl116aa622007-08-15 14:28:22 +00001322
Martin Panteraa0da862015-09-23 05:28:13 +00001323 In enforcing reflexivity of elements, the comparison of collections assumes
1324 that for a collection element ``x``, ``x == x`` is always true. Based on
1325 that assumption, element identity is compared first, and element comparison
1326 is performed only for distinct elements. This approach yields the same
1327 result as a strict element comparison would, if the compared elements are
1328 reflexive. For non-reflexive elements, the result is different than for
1329 strict element comparison, and may be surprising: The non-reflexive
1330 not-a-number values for example result in the following comparison behavior
1331 when used in a list::
1332
1333 >>> nan = float('NaN')
1334 >>> nan is nan
1335 True
1336 >>> nan == nan
1337 False <-- the defined non-reflexive behavior of NaN
1338 >>> [nan] == [nan]
1339 True <-- list enforces reflexivity and tests identity first
1340
1341 Lexicographical comparison between built-in collections works as follows:
1342
1343 - For two collections to compare equal, they must be of the same type, have
1344 the same length, and each pair of corresponding elements must compare
1345 equal (for example, ``[1,2] == (1,2)`` is false because the type is not the
1346 same).
1347
1348 - Collections that support order comparison are ordered the same as their
1349 first unequal elements (for example, ``[1,2,x] <= [1,2,y]`` has the same
1350 value as ``x <= y``). If a corresponding element does not exist, the
1351 shorter collection is ordered first (for example, ``[1,2] < [1,2,3]`` is
1352 true).
1353
1354* Mappings (instances of :class:`dict`) compare equal if and only if they have
cocoatomocdcac032017-03-31 14:48:49 +09001355 equal `(key, value)` pairs. Equality comparison of the keys and values
Martin Panteraa0da862015-09-23 05:28:13 +00001356 enforces reflexivity.
1357
1358 Order comparisons (``<``, ``>``, ``<=``, and ``>=``) raise :exc:`TypeError`.
1359
1360* Sets (instances of :class:`set` or :class:`frozenset`) can be compared within
1361 and across their types.
1362
1363 They define order
1364 comparison operators to mean subset and superset tests. Those relations do
1365 not define total orderings (for example, the two sets ``{1,2}`` and ``{2,3}``
1366 are not equal, nor subsets of one another, nor supersets of one
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001367 another). Accordingly, sets are not appropriate arguments for functions
Martin Panteraa0da862015-09-23 05:28:13 +00001368 which depend on total ordering (for example, :func:`min`, :func:`max`, and
1369 :func:`sorted` produce undefined results given a list of sets as inputs).
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001370
Martin Panteraa0da862015-09-23 05:28:13 +00001371 Comparison of sets enforces reflexivity of its elements.
Georg Brandl116aa622007-08-15 14:28:22 +00001372
Martin Panteraa0da862015-09-23 05:28:13 +00001373* Most other built-in types have no comparison methods implemented, so they
1374 inherit the default comparison behavior.
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001375
Martin Panteraa0da862015-09-23 05:28:13 +00001376User-defined classes that customize their comparison behavior should follow
1377some consistency rules, if possible:
1378
1379* Equality comparison should be reflexive.
1380 In other words, identical objects should compare equal:
1381
1382 ``x is y`` implies ``x == y``
1383
1384* Comparison should be symmetric.
1385 In other words, the following expressions should have the same result:
1386
1387 ``x == y`` and ``y == x``
1388
1389 ``x != y`` and ``y != x``
1390
1391 ``x < y`` and ``y > x``
1392
1393 ``x <= y`` and ``y >= x``
1394
1395* Comparison should be transitive.
1396 The following (non-exhaustive) examples illustrate that:
1397
1398 ``x > y and y > z`` implies ``x > z``
1399
1400 ``x < y and y <= z`` implies ``x < z``
1401
1402* Inverse comparison should result in the boolean negation.
1403 In other words, the following expressions should have the same result:
1404
1405 ``x == y`` and ``not x != y``
1406
1407 ``x < y`` and ``not x >= y`` (for total ordering)
1408
1409 ``x > y`` and ``not x <= y`` (for total ordering)
1410
1411 The last two expressions apply to totally ordered collections (e.g. to
1412 sequences, but not to sets or mappings). See also the
1413 :func:`~functools.total_ordering` decorator.
1414
Martin Panter8dbb0ca2017-01-29 10:00:23 +00001415* The :func:`hash` result should be consistent with equality.
1416 Objects that are equal should either have the same hash value,
1417 or be marked as unhashable.
1418
Martin Panteraa0da862015-09-23 05:28:13 +00001419Python does not enforce these consistency rules. In fact, the not-a-number
1420values are an example for not following these rules.
1421
1422
1423.. _in:
1424.. _not in:
Georg Brandl495f7b52009-10-27 15:28:25 +00001425.. _membership-test-details:
1426
Martin Panteraa0da862015-09-23 05:28:13 +00001427Membership test operations
1428--------------------------
1429
Georg Brandl96593ed2007-09-07 14:15:41 +00001430The operators :keyword:`in` and :keyword:`not in` test for membership. ``x in
Amit Kumar0ae7c8b2017-03-28 19:43:01 +05301431s`` evaluates to ``True`` if *x* is a member of *s*, and ``False`` otherwise.
1432``x not in s`` returns the negation of ``x in s``. All built-in sequences and
1433set types support this as well as dictionary, for which :keyword:`in` tests
1434whether the dictionary has a given key. For container types such as list, tuple,
1435set, frozenset, dict, or collections.deque, the expression ``x in y`` is equivalent
Stefan Krahc8bdc012010-04-01 10:34:09 +00001436to ``any(x is e or x == e for e in y)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001437
Amit Kumar0ae7c8b2017-03-28 19:43:01 +05301438For the string and bytes types, ``x in y`` is ``True`` if and only if *x* is a
Georg Brandl4b491312007-08-31 09:22:56 +00001439substring of *y*. An equivalent test is ``y.find(x) != -1``. Empty strings are
1440always considered to be a substring of any other string, so ``"" in "abc"`` will
1441return ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +00001442
Georg Brandl116aa622007-08-15 14:28:22 +00001443For user-defined classes which define the :meth:`__contains__` method, ``x in
Amit Kumar0ae7c8b2017-03-28 19:43:01 +05301444y`` returns ``True`` if ``y.__contains__(x)`` returns a true value, and
1445``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +00001446
Georg Brandl495f7b52009-10-27 15:28:25 +00001447For user-defined classes which do not define :meth:`__contains__` but do define
Amit Kumar0ae7c8b2017-03-28 19:43:01 +05301448:meth:`__iter__`, ``x in y`` is ``True`` if some value ``z`` with ``x == z`` is
Georg Brandl495f7b52009-10-27 15:28:25 +00001449produced while iterating over ``y``. If an exception is raised during the
1450iteration, it is as if :keyword:`in` raised that exception.
1451
1452Lastly, the old-style iteration protocol is tried: if a class defines
Amit Kumar0ae7c8b2017-03-28 19:43:01 +05301453:meth:`__getitem__`, ``x in y`` is ``True`` if and only if there is a non-negative
Georg Brandl116aa622007-08-15 14:28:22 +00001454integer index *i* such that ``x == y[i]``, and all lower integer indices do not
Georg Brandl96593ed2007-09-07 14:15:41 +00001455raise :exc:`IndexError` exception. (If any other exception is raised, it is as
Georg Brandl116aa622007-08-15 14:28:22 +00001456if :keyword:`in` raised that exception).
1457
1458.. index::
1459 operator: in
1460 operator: not in
1461 pair: membership; test
1462 object: sequence
1463
1464The operator :keyword:`not in` is defined to have the inverse true value of
1465:keyword:`in`.
1466
1467.. index::
1468 operator: is
1469 operator: is not
1470 pair: identity; test
1471
Martin Panteraa0da862015-09-23 05:28:13 +00001472
1473.. _is:
1474.. _is not:
1475
1476Identity comparisons
1477--------------------
1478
Georg Brandl116aa622007-08-15 14:28:22 +00001479The operators :keyword:`is` and :keyword:`is not` test for object identity: ``x
Raymond Hettinger06e18a72016-09-11 17:23:49 -07001480is y`` is true if and only if *x* and *y* are the same object. Object identity
1481is determined using the :meth:`id` function. ``x is not y`` yields the inverse
1482truth value. [#]_
Georg Brandl116aa622007-08-15 14:28:22 +00001483
1484
1485.. _booleans:
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001486.. _and:
1487.. _or:
1488.. _not:
Georg Brandl116aa622007-08-15 14:28:22 +00001489
1490Boolean operations
1491==================
1492
1493.. index::
1494 pair: Conditional; expression
1495 pair: Boolean; operation
1496
Georg Brandl116aa622007-08-15 14:28:22 +00001497.. productionlist::
Georg Brandl116aa622007-08-15 14:28:22 +00001498 or_test: `and_test` | `or_test` "or" `and_test`
1499 and_test: `not_test` | `and_test` "and" `not_test`
1500 not_test: `comparison` | "not" `not_test`
1501
1502In the context of Boolean operations, and also when expressions are used by
1503control flow statements, the following values are interpreted as false:
1504``False``, ``None``, numeric zero of all types, and empty strings and containers
1505(including strings, tuples, lists, dictionaries, sets and frozensets). All
Georg Brandl96593ed2007-09-07 14:15:41 +00001506other values are interpreted as true. User-defined objects can customize their
1507truth value by providing a :meth:`__bool__` method.
Georg Brandl116aa622007-08-15 14:28:22 +00001508
1509.. index:: operator: not
1510
1511The operator :keyword:`not` yields ``True`` if its argument is false, ``False``
1512otherwise.
1513
Georg Brandl116aa622007-08-15 14:28:22 +00001514.. index:: operator: and
1515
1516The expression ``x and y`` first evaluates *x*; if *x* is false, its value is
1517returned; otherwise, *y* is evaluated and the resulting value is returned.
1518
1519.. index:: operator: or
1520
1521The expression ``x or y`` first evaluates *x*; if *x* is true, its value is
1522returned; otherwise, *y* is evaluated and the resulting value is returned.
1523
1524(Note that neither :keyword:`and` nor :keyword:`or` restrict the value and type
1525they return to ``False`` and ``True``, but rather return the last evaluated
Georg Brandl96593ed2007-09-07 14:15:41 +00001526argument. This is sometimes useful, e.g., if ``s`` is a string that should be
Georg Brandl116aa622007-08-15 14:28:22 +00001527replaced by a default value if it is empty, the expression ``s or 'foo'`` yields
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001528the desired value. Because :keyword:`not` has to create a new value, it
1529returns a boolean value regardless of the type of its argument
1530(for example, ``not 'foo'`` produces ``False`` rather than ``''``.)
Georg Brandl116aa622007-08-15 14:28:22 +00001531
1532
Alexander Belopolsky50ba19e2010-12-15 19:47:37 +00001533Conditional expressions
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001534=======================
1535
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001536.. index::
1537 pair: conditional; expression
1538 pair: ternary; operator
1539
1540.. productionlist::
1541 conditional_expression: `or_test` ["if" `or_test` "else" `expression`]
Georg Brandl242e6a02013-10-06 10:28:39 +02001542 expression: `conditional_expression` | `lambda_expr`
1543 expression_nocond: `or_test` | `lambda_expr_nocond`
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001544
1545Conditional expressions (sometimes called a "ternary operator") have the lowest
1546priority of all Python operations.
1547
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001548The expression ``x if C else y`` first evaluates the condition, *C* rather than *x*.
1549If *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001550evaluated and its value is returned.
1551
1552See :pep:`308` for more details about conditional expressions.
1553
1554
Georg Brandl116aa622007-08-15 14:28:22 +00001555.. _lambdas:
Georg Brandlc4f8b242009-04-10 08:17:21 +00001556.. _lambda:
Georg Brandl116aa622007-08-15 14:28:22 +00001557
1558Lambdas
1559=======
1560
1561.. index::
1562 pair: lambda; expression
1563 pair: lambda; form
1564 pair: anonymous; function
1565
1566.. productionlist::
Georg Brandl242e6a02013-10-06 10:28:39 +02001567 lambda_expr: "lambda" [`parameter_list`]: `expression`
1568 lambda_expr_nocond: "lambda" [`parameter_list`]: `expression_nocond`
Georg Brandl116aa622007-08-15 14:28:22 +00001569
Zachary Ware2f78b842014-06-03 09:32:40 -05001570Lambda expressions (sometimes called lambda forms) are used to create anonymous
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001571functions. The expression ``lambda arguments: expression`` yields a function
Martin Panter1050d2d2016-07-26 11:18:21 +02001572object. The unnamed object behaves like a function object defined with:
1573
1574.. code-block:: none
Georg Brandl116aa622007-08-15 14:28:22 +00001575
Georg Brandl96593ed2007-09-07 14:15:41 +00001576 def <lambda>(arguments):
Georg Brandl116aa622007-08-15 14:28:22 +00001577 return expression
1578
1579See section :ref:`function` for the syntax of parameter lists. Note that
Georg Brandl242e6a02013-10-06 10:28:39 +02001580functions created with lambda expressions cannot contain statements or
1581annotations.
Georg Brandl116aa622007-08-15 14:28:22 +00001582
Georg Brandl116aa622007-08-15 14:28:22 +00001583
1584.. _exprlists:
1585
1586Expression lists
1587================
1588
1589.. index:: pair: expression; list
1590
1591.. productionlist::
1592 expression_list: `expression` ( "," `expression` )* [","]
Martin Panter0c0da482016-06-12 01:46:50 +00001593 starred_list: `starred_item` ( "," `starred_item` )* [","]
1594 starred_expression: `expression` | ( `starred_item` "," )* [`starred_item`]
1595 starred_item: `expression` | "*" `or_expr`
Georg Brandl116aa622007-08-15 14:28:22 +00001596
1597.. index:: object: tuple
1598
Martin Panter0c0da482016-06-12 01:46:50 +00001599Except when part of a list or set display, an expression list
1600containing at least one comma yields a tuple. The length of
Georg Brandl116aa622007-08-15 14:28:22 +00001601the tuple is the number of expressions in the list. The expressions are
1602evaluated from left to right.
1603
Martin Panter0c0da482016-06-12 01:46:50 +00001604.. index::
1605 pair: iterable; unpacking
1606 single: *; in expression lists
1607
1608An asterisk ``*`` denotes :dfn:`iterable unpacking`. Its operand must be
1609an :term:`iterable`. The iterable is expanded into a sequence of items,
1610which are included in the new tuple, list, or set, at the site of
1611the unpacking.
1612
1613.. versionadded:: 3.5
1614 Iterable unpacking in expression lists, originally proposed by :pep:`448`.
1615
Georg Brandl116aa622007-08-15 14:28:22 +00001616.. index:: pair: trailing; comma
1617
1618The trailing comma is required only to create a single tuple (a.k.a. a
1619*singleton*); it is optional in all other cases. A single expression without a
1620trailing comma doesn't create a tuple, but rather yields the value of that
1621expression. (To create an empty tuple, use an empty pair of parentheses:
1622``()``.)
1623
1624
1625.. _evalorder:
1626
1627Evaluation order
1628================
1629
1630.. index:: pair: evaluation; order
1631
Georg Brandl96593ed2007-09-07 14:15:41 +00001632Python evaluates expressions from left to right. Notice that while evaluating
1633an assignment, the right-hand side is evaluated before the left-hand side.
Georg Brandl116aa622007-08-15 14:28:22 +00001634
1635In the following lines, expressions will be evaluated in the arithmetic order of
1636their suffixes::
1637
1638 expr1, expr2, expr3, expr4
1639 (expr1, expr2, expr3, expr4)
1640 {expr1: expr2, expr3: expr4}
1641 expr1 + expr2 * (expr3 - expr4)
Georg Brandl734e2682008-08-12 08:18:18 +00001642 expr1(expr2, expr3, *expr4, **expr5)
Georg Brandl116aa622007-08-15 14:28:22 +00001643 expr3, expr4 = expr1, expr2
1644
1645
1646.. _operator-summary:
1647
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001648Operator precedence
1649===================
Georg Brandl116aa622007-08-15 14:28:22 +00001650
1651.. index:: pair: operator; precedence
1652
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001653The following table summarizes the operator precedence in Python, from lowest
Georg Brandl96593ed2007-09-07 14:15:41 +00001654precedence (least binding) to highest precedence (most binding). Operators in
Georg Brandl116aa622007-08-15 14:28:22 +00001655the same box have the same precedence. Unless the syntax is explicitly given,
1656operators are binary. Operators in the same box group left to right (except for
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001657exponentiation, which groups from right to left).
1658
1659Note that comparisons, membership tests, and identity tests, all have the same
1660precedence and have a left-to-right chaining feature as described in the
1661:ref:`comparisons` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001662
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001663
1664+-----------------------------------------------+-------------------------------------+
1665| Operator | Description |
1666+===============================================+=====================================+
1667| :keyword:`lambda` | Lambda expression |
1668+-----------------------------------------------+-------------------------------------+
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001669| :keyword:`if` -- :keyword:`else` | Conditional expression |
1670+-----------------------------------------------+-------------------------------------+
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001671| :keyword:`or` | Boolean OR |
1672+-----------------------------------------------+-------------------------------------+
1673| :keyword:`and` | Boolean AND |
1674+-----------------------------------------------+-------------------------------------+
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001675| :keyword:`not` ``x`` | Boolean NOT |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001676+-----------------------------------------------+-------------------------------------+
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001677| :keyword:`in`, :keyword:`not in`, | Comparisons, including membership |
Georg Brandl44ea77b2013-03-28 13:28:44 +01001678| :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests |
Georg Brandla5ebc262009-06-03 07:26:22 +00001679| ``<=``, ``>``, ``>=``, ``!=``, ``==`` | |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001680+-----------------------------------------------+-------------------------------------+
1681| ``|`` | Bitwise OR |
1682+-----------------------------------------------+-------------------------------------+
1683| ``^`` | Bitwise XOR |
1684+-----------------------------------------------+-------------------------------------+
1685| ``&`` | Bitwise AND |
1686+-----------------------------------------------+-------------------------------------+
1687| ``<<``, ``>>`` | Shifts |
1688+-----------------------------------------------+-------------------------------------+
1689| ``+``, ``-`` | Addition and subtraction |
1690+-----------------------------------------------+-------------------------------------+
Benjamin Petersond51374e2014-04-09 23:55:56 -04001691| ``*``, ``@``, ``/``, ``//``, ``%`` | Multiplication, matrix |
svelankar9b47af62017-09-17 20:56:16 -04001692| | multiplication, division, floor |
1693| | division, remainder [#]_ |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001694+-----------------------------------------------+-------------------------------------+
1695| ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT |
1696+-----------------------------------------------+-------------------------------------+
1697| ``**`` | Exponentiation [#]_ |
1698+-----------------------------------------------+-------------------------------------+
Yury Selivanovf3e40fa2015-05-21 11:50:30 -04001699| ``await`` ``x`` | Await expression |
1700+-----------------------------------------------+-------------------------------------+
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001701| ``x[index]``, ``x[index:index]``, | Subscription, slicing, |
1702| ``x(arguments...)``, ``x.attribute`` | call, attribute reference |
1703+-----------------------------------------------+-------------------------------------+
1704| ``(expressions...)``, | Binding or tuple display, |
1705| ``[expressions...]``, | list display, |
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001706| ``{key: value...}``, | dictionary display, |
Brett Cannon925914f2010-11-21 19:58:24 +00001707| ``{expressions...}`` | set display |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001708+-----------------------------------------------+-------------------------------------+
1709
Georg Brandl116aa622007-08-15 14:28:22 +00001710
1711.. rubric:: Footnotes
1712
Georg Brandl116aa622007-08-15 14:28:22 +00001713.. [#] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it may not be
1714 true numerically due to roundoff. For example, and assuming a platform on which
1715 a Python float is an IEEE 754 double-precision number, in order that ``-1e-100 %
1716 1e100`` have the same sign as ``1e100``, the computed result is ``-1e-100 +
Georg Brandl063f2372010-12-01 15:32:43 +00001717 1e100``, which is numerically exactly equal to ``1e100``. The function
1718 :func:`math.fmod` returns a result whose sign matches the sign of the
Georg Brandl116aa622007-08-15 14:28:22 +00001719 first argument instead, and so returns ``-1e-100`` in this case. Which approach
1720 is more appropriate depends on the application.
1721
1722.. [#] If x is very close to an exact integer multiple of y, it's possible for
Georg Brandl96593ed2007-09-07 14:15:41 +00001723 ``x//y`` to be one larger than ``(x-x%y)//y`` due to rounding. In such
Georg Brandl116aa622007-08-15 14:28:22 +00001724 cases, Python returns the latter result, in order to preserve that
1725 ``divmod(x,y)[0] * y + x % y`` be very close to ``x``.
1726
Martin Panteraa0da862015-09-23 05:28:13 +00001727.. [#] The Unicode standard distinguishes between :dfn:`code points`
1728 (e.g. U+0041) and :dfn:`abstract characters` (e.g. "LATIN CAPITAL LETTER A").
1729 While most abstract characters in Unicode are only represented using one
1730 code point, there is a number of abstract characters that can in addition be
1731 represented using a sequence of more than one code point. For example, the
1732 abstract character "LATIN CAPITAL LETTER C WITH CEDILLA" can be represented
1733 as a single :dfn:`precomposed character` at code position U+00C7, or as a
1734 sequence of a :dfn:`base character` at code position U+0043 (LATIN CAPITAL
1735 LETTER C), followed by a :dfn:`combining character` at code position U+0327
1736 (COMBINING CEDILLA).
1737
1738 The comparison operators on strings compare at the level of Unicode code
1739 points. This may be counter-intuitive to humans. For example,
1740 ``"\u00C7" == "\u0043\u0327"`` is ``False``, even though both strings
1741 represent the same abstract character "LATIN CAPITAL LETTER C WITH CEDILLA".
1742
1743 To compare strings at the level of abstract characters (that is, in a way
1744 intuitive to humans), use :func:`unicodedata.normalize`.
Guido van Rossumda27fd22007-08-17 00:24:54 +00001745
Georg Brandl48310cd2009-01-03 21:18:54 +00001746.. [#] Due to automatic garbage-collection, free lists, and the dynamic nature of
Benjamin Peterson41181742008-07-02 20:22:54 +00001747 descriptors, you may notice seemingly unusual behaviour in certain uses of
1748 the :keyword:`is` operator, like those involving comparisons between instance
1749 methods, or constants. Check their documentation for more info.
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001750
Georg Brandl063f2372010-12-01 15:32:43 +00001751.. [#] The ``%`` operator is also used for string formatting; the same
1752 precedence applies.
Georg Brandlf1d633c2010-09-20 06:29:01 +00001753
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001754.. [#] The power operator ``**`` binds less tightly than an arithmetic or
1755 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``.