blob: eafd70a8352b9b89126a27a1c236ec5058eac66c [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`
175 comp_for: "for" `target_list` "in" `or_test` [`comp_iter`]
176 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
Georg Brandl96593ed2007-09-07 14:15:41 +0000189
Georg Brandl116aa622007-08-15 14:28:22 +0000190.. _lists:
191
192List displays
193-------------
194
195.. index::
196 pair: list; display
197 pair: list; comprehensions
Georg Brandl96593ed2007-09-07 14:15:41 +0000198 pair: empty; list
199 object: list
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201A list display is a possibly empty series of expressions enclosed in square
202brackets:
203
204.. productionlist::
Martin Panter0c0da482016-06-12 01:46:50 +0000205 list_display: "[" [`starred_list` | `comprehension`] "]"
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Georg Brandl96593ed2007-09-07 14:15:41 +0000207A list display yields a new list object, the contents being specified by either
208a list of expressions or a comprehension. When a comma-separated list of
209expressions is supplied, its elements are evaluated from left to right and
210placed into the list object in that order. When a comprehension is supplied,
211the list is constructed from the elements resulting from the comprehension.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213
Georg Brandl96593ed2007-09-07 14:15:41 +0000214.. _set:
Georg Brandl116aa622007-08-15 14:28:22 +0000215
Georg Brandl96593ed2007-09-07 14:15:41 +0000216Set displays
217------------
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Georg Brandl96593ed2007-09-07 14:15:41 +0000219.. index:: pair: set; display
220 object: set
Georg Brandl116aa622007-08-15 14:28:22 +0000221
Georg Brandl96593ed2007-09-07 14:15:41 +0000222A set display is denoted by curly braces and distinguishable from dictionary
223displays by the lack of colons separating keys and values:
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225.. productionlist::
Martin Panter0c0da482016-06-12 01:46:50 +0000226 set_display: "{" (`starred_list` | `comprehension`) "}"
Georg Brandl116aa622007-08-15 14:28:22 +0000227
Georg Brandl96593ed2007-09-07 14:15:41 +0000228A set display yields a new mutable set object, the contents being specified by
229either a sequence of expressions or a comprehension. When a comma-separated
230list of expressions is supplied, its elements are evaluated from left to right
231and added to the set object. When a comprehension is supplied, the set is
232constructed from the elements resulting from the comprehension.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Georg Brandl528cdb12008-09-21 07:09:51 +0000234An empty set cannot be constructed with ``{}``; this literal constructs an empty
235dictionary.
Christian Heimes78644762008-03-04 23:39:23 +0000236
237
Georg Brandl116aa622007-08-15 14:28:22 +0000238.. _dict:
239
240Dictionary displays
241-------------------
242
243.. index:: pair: dictionary; display
Georg Brandl96593ed2007-09-07 14:15:41 +0000244 key, datum, key/datum pair
245 object: dictionary
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247A dictionary display is a possibly empty series of key/datum pairs enclosed in
248curly braces:
249
250.. productionlist::
Georg Brandl96593ed2007-09-07 14:15:41 +0000251 dict_display: "{" [`key_datum_list` | `dict_comprehension`] "}"
Georg Brandl116aa622007-08-15 14:28:22 +0000252 key_datum_list: `key_datum` ("," `key_datum`)* [","]
Martin Panter0c0da482016-06-12 01:46:50 +0000253 key_datum: `expression` ":" `expression` | "**" `or_expr`
Georg Brandl96593ed2007-09-07 14:15:41 +0000254 dict_comprehension: `expression` ":" `expression` `comp_for`
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256A dictionary display yields a new dictionary object.
257
Georg Brandl96593ed2007-09-07 14:15:41 +0000258If a comma-separated sequence of key/datum pairs is given, they are evaluated
259from left to right to define the entries of the dictionary: each key object is
260used as a key into the dictionary to store the corresponding datum. This means
261that you can specify the same key multiple times in the key/datum list, and the
262final dictionary's value for that key will be the last one given.
263
Martin Panter0c0da482016-06-12 01:46:50 +0000264.. index:: unpacking; dictionary, **; in dictionary displays
265
266A double asterisk ``**`` denotes :dfn:`dictionary unpacking`.
267Its operand must be a :term:`mapping`. Each mapping item is added
268to the new dictionary. Later values replace values already set by
269earlier key/datum pairs and earlier dictionary unpackings.
270
271.. versionadded:: 3.5
272 Unpacking into dictionary displays, originally proposed by :pep:`448`.
273
Georg Brandl96593ed2007-09-07 14:15:41 +0000274A dict comprehension, in contrast to list and set comprehensions, needs two
275expressions separated with a colon followed by the usual "for" and "if" clauses.
276When the comprehension is run, the resulting key and value elements are inserted
277in the new dictionary in the order they are produced.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279.. index:: pair: immutable; object
Georg Brandl96593ed2007-09-07 14:15:41 +0000280 hashable
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282Restrictions on the types of the key values are listed earlier in section
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000283:ref:`types`. (To summarize, the key type should be :term:`hashable`, which excludes
Georg Brandl116aa622007-08-15 14:28:22 +0000284all mutable objects.) Clashes between duplicate keys are not detected; the last
285datum (textually rightmost in the display) stored for a given key value
286prevails.
287
288
Georg Brandl96593ed2007-09-07 14:15:41 +0000289.. _genexpr:
290
291Generator expressions
292---------------------
293
294.. index:: pair: generator; expression
295 object: generator
296
297A generator expression is a compact generator notation in parentheses:
298
299.. productionlist::
300 generator_expression: "(" `expression` `comp_for` ")"
301
302A generator expression yields a new generator object. Its syntax is the same as
303for comprehensions, except that it is enclosed in parentheses instead of
304brackets or curly braces.
305
306Variables used in the generator expression are evaluated lazily when the
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700307:meth:`~generator.__next__` method is called for the generator object (in the same
Ezio Melotti7fa82222012-10-12 13:42:08 +0300308fashion as normal generators). However, the leftmost :keyword:`for` clause is
309immediately evaluated, so that an error produced by it can be seen before any
310other possible error in the code that handles the generator expression.
311Subsequent :keyword:`for` clauses cannot be evaluated immediately since they
312may depend on the previous :keyword:`for` loop. For example: ``(x*y for x in
313range(10) for y in bar(x))``.
Georg Brandl96593ed2007-09-07 14:15:41 +0000314
315The parentheses can be omitted on calls with only one argument. See section
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700316:ref:`calls` for details.
Georg Brandl96593ed2007-09-07 14:15:41 +0000317
318
Georg Brandl116aa622007-08-15 14:28:22 +0000319.. _yieldexpr:
320
321Yield expressions
322-----------------
323
324.. index::
325 keyword: yield
326 pair: yield; expression
327 pair: generator; function
328
329.. productionlist::
330 yield_atom: "(" `yield_expression` ")"
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000331 yield_expression: "yield" [`expression_list` | "from" `expression`]
Georg Brandl116aa622007-08-15 14:28:22 +0000332
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500333The yield expression is only used when defining a :term:`generator` function and
334thus can only be used in the body of a function definition. Using a yield
335expression in a function's body causes that function to be a generator.
Georg Brandl116aa622007-08-15 14:28:22 +0000336
337When a generator function is called, it returns an iterator known as a
Guido van Rossumd0150ad2015-05-05 12:02:01 -0700338generator. That generator then controls the execution of the generator function.
Georg Brandl116aa622007-08-15 14:28:22 +0000339The execution starts when one of the generator's methods is called. At that
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500340time, the execution proceeds to the first yield expression, where it is
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700341suspended again, returning the value of :token:`expression_list` to the generator's
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500342caller. By suspended, we mean that all local state is retained, including the
Ethan Furman2f825af2015-01-14 22:25:27 -0800343current bindings of local variables, the instruction pointer, the internal
344evaluation stack, and the state of any exception handling. When the execution
345is resumed by calling one of the
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500346generator's methods, the function can proceed exactly as if the yield expression
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700347were just another external call. The value of the yield expression after
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500348resuming depends on the method which resumed the execution. If
349:meth:`~generator.__next__` is used (typically via either a :keyword:`for` or
350the :func:`next` builtin) then the result is :const:`None`. Otherwise, if
351:meth:`~generator.send` is used, then the result will be the value passed in to
352that method.
Georg Brandl116aa622007-08-15 14:28:22 +0000353
354.. index:: single: coroutine
355
356All of this makes generator functions quite similar to coroutines; they yield
357multiple times, they have more than one entry point and their execution can be
358suspended. The only difference is that a generator function cannot control
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700359where the execution should continue after it yields; the control is always
Georg Brandl6faee4e2010-09-21 14:48:28 +0000360transferred to the generator's caller.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Ethan Furman2f825af2015-01-14 22:25:27 -0800362Yield expressions are allowed anywhere in a :keyword:`try` construct. If the
363generator is not resumed before it is
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500364finalized (by reaching a zero reference count or by being garbage collected),
365the generator-iterator's :meth:`~generator.close` method will be called,
366allowing any pending :keyword:`finally` clauses to execute.
Georg Brandl02c30562007-09-07 17:52:53 +0000367
Nick Coghlan0ed80192012-01-14 14:43:24 +1000368When ``yield from <expr>`` is used, it treats the supplied expression as
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000369a subiterator. All values produced by that subiterator are passed directly
370to the caller of the current generator's methods. Any values passed in with
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +0300371:meth:`~generator.send` and any exceptions passed in with
372:meth:`~generator.throw` are passed to the underlying iterator if it has the
373appropriate methods. If this is not the case, then :meth:`~generator.send`
374will raise :exc:`AttributeError` or :exc:`TypeError`, while
375:meth:`~generator.throw` will just raise the passed in exception immediately.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000376
377When the underlying iterator is complete, the :attr:`~StopIteration.value`
378attribute of the raised :exc:`StopIteration` instance becomes the value of
379the yield expression. It can be either set explicitly when raising
380:exc:`StopIteration`, or automatically when the sub-iterator is a generator
381(by returning a value from the sub-generator).
382
Nick Coghlan0ed80192012-01-14 14:43:24 +1000383 .. versionchanged:: 3.3
Martin Panterd21e0b52015-10-10 10:36:22 +0000384 Added ``yield from <expr>`` to delegate control flow to a subiterator.
Nick Coghlan0ed80192012-01-14 14:43:24 +1000385
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500386The parentheses may be omitted when the yield expression is the sole expression
387on the right hand side of an assignment statement.
388
389.. seealso::
390
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300391 :pep:`255` - Simple Generators
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500392 The proposal for adding generators and the :keyword:`yield` statement to Python.
393
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300394 :pep:`342` - Coroutines via Enhanced Generators
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500395 The proposal to enhance the API and syntax of generators, making them
396 usable as simple coroutines.
397
Serhiy Storchakae4ba8722016-03-31 15:30:54 +0300398 :pep:`380` - Syntax for Delegating to a Subgenerator
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500399 The proposal to introduce the :token:`yield_from` syntax, making delegation
400 to sub-generators easy.
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000401
Georg Brandl116aa622007-08-15 14:28:22 +0000402.. index:: object: generator
Yury Selivanov66f88282015-06-24 11:04:15 -0400403.. _generator-methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000404
R David Murray2c1d1d62012-08-17 20:48:59 -0400405Generator-iterator methods
406^^^^^^^^^^^^^^^^^^^^^^^^^^
407
408This subsection describes the methods of a generator iterator. They can
409be used to control the execution of a generator function.
410
411Note that calling any of the generator methods below when the generator
412is already executing raises a :exc:`ValueError` exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000413
414.. index:: exception: StopIteration
415
416
Georg Brandl96593ed2007-09-07 14:15:41 +0000417.. method:: generator.__next__()
Georg Brandl116aa622007-08-15 14:28:22 +0000418
Georg Brandl96593ed2007-09-07 14:15:41 +0000419 Starts the execution of a generator function or resumes it at the last
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500420 executed yield expression. When a generator function is resumed with a
421 :meth:`~generator.__next__` method, the current yield expression always
422 evaluates to :const:`None`. The execution then continues to the next yield
423 expression, where the generator is suspended again, and the value of the
Serhiy Storchaka848c8b22014-09-05 23:27:36 +0300424 :token:`expression_list` is returned to :meth:`__next__`'s caller. If the
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500425 generator exits without yielding another value, a :exc:`StopIteration`
Georg Brandl96593ed2007-09-07 14:15:41 +0000426 exception is raised.
427
428 This method is normally called implicitly, e.g. by a :keyword:`for` loop, or
429 by the built-in :func:`next` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431
432.. method:: generator.send(value)
433
434 Resumes the execution and "sends" a value into the generator function. The
Benjamin Petersond1c85fd2014-01-26 22:52:08 -0500435 *value* argument becomes the result of the current yield expression. The
436 :meth:`send` method returns the next value yielded by the generator, or
437 raises :exc:`StopIteration` if the generator exits without yielding another
438 value. When :meth:`send` is called to start the generator, it must be called
439 with :const:`None` as the argument, because there is no yield expression that
440 could receive the value.
Georg Brandl116aa622007-08-15 14:28:22 +0000441
442
443.. method:: generator.throw(type[, value[, traceback]])
444
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700445 Raises an exception of type ``type`` at the point where the generator was paused,
Georg Brandl116aa622007-08-15 14:28:22 +0000446 and returns the next value yielded by the generator function. If the generator
447 exits without yielding another value, a :exc:`StopIteration` exception is
448 raised. If the generator function does not catch the passed-in exception, or
449 raises a different exception, then that exception propagates to the caller.
450
451.. index:: exception: GeneratorExit
452
453
454.. method:: generator.close()
455
456 Raises a :exc:`GeneratorExit` at the point where the generator function was
Yury Selivanov8170e8c2015-05-09 11:44:30 -0400457 paused. If the generator function then exits gracefully, is already closed,
458 or raises :exc:`GeneratorExit` (by not catching the exception), close
459 returns to its caller. If the generator yields a value, a
460 :exc:`RuntimeError` is raised. If the generator raises any other exception,
461 it is propagated to the caller. :meth:`close` does nothing if the generator
462 has already exited due to an exception or normal exit.
Georg Brandl116aa622007-08-15 14:28:22 +0000463
Chris Jerdonek2654b862012-12-23 15:31:57 -0800464.. index:: single: yield; examples
465
466Examples
467^^^^^^^^
468
Georg Brandl116aa622007-08-15 14:28:22 +0000469Here is a simple example that demonstrates the behavior of generators and
470generator functions::
471
472 >>> def echo(value=None):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000473 ... print("Execution starts when 'next()' is called for the first time.")
Georg Brandl116aa622007-08-15 14:28:22 +0000474 ... try:
475 ... while True:
476 ... try:
477 ... value = (yield value)
Georg Brandlfe800a32009-08-03 17:50:20 +0000478 ... except Exception as e:
Georg Brandl116aa622007-08-15 14:28:22 +0000479 ... value = e
480 ... finally:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000481 ... print("Don't forget to clean up when 'close()' is called.")
Georg Brandl116aa622007-08-15 14:28:22 +0000482 ...
483 >>> generator = echo(1)
Georg Brandl96593ed2007-09-07 14:15:41 +0000484 >>> print(next(generator))
Georg Brandl116aa622007-08-15 14:28:22 +0000485 Execution starts when 'next()' is called for the first time.
486 1
Georg Brandl96593ed2007-09-07 14:15:41 +0000487 >>> print(next(generator))
Georg Brandl116aa622007-08-15 14:28:22 +0000488 None
Georg Brandl6911e3c2007-09-04 07:15:32 +0000489 >>> print(generator.send(2))
Georg Brandl116aa622007-08-15 14:28:22 +0000490 2
491 >>> generator.throw(TypeError, "spam")
492 TypeError('spam',)
493 >>> generator.close()
494 Don't forget to clean up when 'close()' is called.
495
Chris Jerdonek2654b862012-12-23 15:31:57 -0800496For examples using ``yield from``, see :ref:`pep-380` in "What's New in
497Python."
498
Georg Brandl116aa622007-08-15 14:28:22 +0000499
Georg Brandl116aa622007-08-15 14:28:22 +0000500.. _primaries:
501
502Primaries
503=========
504
505.. index:: single: primary
506
507Primaries represent the most tightly bound operations of the language. Their
508syntax is:
509
510.. productionlist::
511 primary: `atom` | `attributeref` | `subscription` | `slicing` | `call`
512
513
514.. _attribute-references:
515
516Attribute references
517--------------------
518
519.. index:: pair: attribute; reference
520
521An attribute reference is a primary followed by a period and a name:
522
523.. productionlist::
524 attributeref: `primary` "." `identifier`
525
526.. index::
527 exception: AttributeError
528 object: module
529 object: list
530
531The primary must evaluate to an object of a type that supports attribute
Georg Brandl96593ed2007-09-07 14:15:41 +0000532references, which most objects do. This object is then asked to produce the
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700533attribute whose name is the identifier. This production can be customized by
Zachary Ware2f78b842014-06-03 09:32:40 -0500534overriding the :meth:`__getattr__` method. If this attribute is not available,
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700535the exception :exc:`AttributeError` is raised. Otherwise, the type and value of
536the object produced is determined by the object. Multiple evaluations of the
537same attribute reference may yield different objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000538
539
540.. _subscriptions:
541
542Subscriptions
543-------------
544
545.. index:: single: subscription
546
547.. index::
548 object: sequence
549 object: mapping
550 object: string
551 object: tuple
552 object: list
553 object: dictionary
554 pair: sequence; item
555
556A subscription selects an item of a sequence (string, tuple or list) or mapping
557(dictionary) object:
558
559.. productionlist::
560 subscription: `primary` "[" `expression_list` "]"
561
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700562The primary must evaluate to an object that supports subscription (lists or
563dictionaries for example). User-defined objects can support subscription by
564defining a :meth:`__getitem__` method.
Georg Brandl96593ed2007-09-07 14:15:41 +0000565
566For built-in objects, there are two types of objects that support subscription:
Georg Brandl116aa622007-08-15 14:28:22 +0000567
568If the primary is a mapping, the expression list must evaluate to an object
569whose value is one of the keys of the mapping, and the subscription selects the
570value in the mapping that corresponds to that key. (The expression list is a
571tuple except if it has exactly one item.)
572
Raymond Hettingerf77c1d62010-09-15 00:09:26 +0000573If the primary is a sequence, the expression (list) must evaluate to an integer
574or a slice (as discussed in the following section).
575
576The formal syntax makes no special provision for negative indices in
577sequences; however, built-in sequences all provide a :meth:`__getitem__`
578method that interprets negative indices by adding the length of the sequence
579to the index (so that ``x[-1]`` selects the last item of ``x``). The
580resulting value must be a nonnegative integer less than the number of items in
581the sequence, and the subscription selects the item whose index is that value
582(counting from zero). Since the support for negative indices and slicing
583occurs in the object's :meth:`__getitem__` method, subclasses overriding
584this method will need to explicitly add that support.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586.. index::
587 single: character
588 pair: string; item
589
590A string's items are characters. A character is not a separate data type but a
591string of exactly one character.
592
593
594.. _slicings:
595
596Slicings
597--------
598
599.. index::
600 single: slicing
601 single: slice
602
603.. index::
604 object: sequence
605 object: string
606 object: tuple
607 object: list
608
609A slicing selects a range of items in a sequence object (e.g., a string, tuple
610or list). Slicings may be used as expressions or as targets in assignment or
611:keyword:`del` statements. The syntax for a slicing:
612
613.. productionlist::
Georg Brandl48310cd2009-01-03 21:18:54 +0000614 slicing: `primary` "[" `slice_list` "]"
Georg Brandl116aa622007-08-15 14:28:22 +0000615 slice_list: `slice_item` ("," `slice_item`)* [","]
Georg Brandlcb8ecb12007-09-04 06:35:14 +0000616 slice_item: `expression` | `proper_slice`
Thomas Wouters53de1902007-09-04 09:03:59 +0000617 proper_slice: [`lower_bound`] ":" [`upper_bound`] [ ":" [`stride`] ]
Georg Brandl116aa622007-08-15 14:28:22 +0000618 lower_bound: `expression`
619 upper_bound: `expression`
620 stride: `expression`
Georg Brandl116aa622007-08-15 14:28:22 +0000621
622There is ambiguity in the formal syntax here: anything that looks like an
623expression list also looks like a slice list, so any subscription can be
624interpreted as a slicing. Rather than further complicating the syntax, this is
625disambiguated by defining that in this case the interpretation as a subscription
626takes priority over the interpretation as a slicing (this is the case if the
Thomas Wouters53de1902007-09-04 09:03:59 +0000627slice list contains no proper slice).
Georg Brandl116aa622007-08-15 14:28:22 +0000628
629.. index::
630 single: start (slice object attribute)
631 single: stop (slice object attribute)
632 single: step (slice object attribute)
633
Georg Brandla4c8c472014-10-31 10:38:49 +0100634The semantics for a slicing are as follows. The primary is indexed (using the
635same :meth:`__getitem__` method as
Georg Brandl96593ed2007-09-07 14:15:41 +0000636normal subscription) with a key that is constructed from the slice list, as
637follows. If the slice list contains at least one comma, the key is a tuple
638containing the conversion of the slice items; otherwise, the conversion of the
639lone slice item is the key. The conversion of a slice item that is an
640expression is that expression. The conversion of a proper slice is a slice
Serhiy Storchaka0d196ed2013-10-09 14:02:31 +0300641object (see section :ref:`types`) whose :attr:`~slice.start`,
642:attr:`~slice.stop` and :attr:`~slice.step` attributes are the values of the
643expressions given as lower bound, upper bound and stride, respectively,
644substituting ``None`` for missing expressions.
Georg Brandl116aa622007-08-15 14:28:22 +0000645
646
Chris Jerdonekb4309942012-12-25 14:54:44 -0800647.. index::
648 object: callable
649 single: call
650 single: argument; call semantics
651
Georg Brandl116aa622007-08-15 14:28:22 +0000652.. _calls:
653
654Calls
655-----
656
Chris Jerdonekb4309942012-12-25 14:54:44 -0800657A call calls a callable object (e.g., a :term:`function`) with a possibly empty
658series of :term:`arguments <argument>`:
Georg Brandl116aa622007-08-15 14:28:22 +0000659
660.. productionlist::
Georg Brandldc529c12008-09-21 17:03:29 +0000661 call: `primary` "(" [`argument_list` [","] | `comprehension`] ")"
Martin Panter0c0da482016-06-12 01:46:50 +0000662 argument_list: `positional_arguments` ["," `starred_and_keywords`]
663 : ["," `keywords_arguments`]
664 : | `starred_and_keywords` ["," `keywords_arguments`]
665 : | `keywords_arguments`
666 positional_arguments: ["*"] `expression` ("," ["*"] `expression`)*
667 starred_and_keywords: ("*" `expression` | `keyword_item`)
668 : ("," "*" `expression` | "," `keyword_item`)*
669 keywords_arguments: (`keyword_item` | "**" `expression`)
670 : ("," `keyword_item` | "**" `expression`)*
Georg Brandl116aa622007-08-15 14:28:22 +0000671 keyword_item: `identifier` "=" `expression`
672
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700673An optional trailing comma may be present after the positional and keyword arguments
674but does not affect the semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000675
Chris Jerdonekb4309942012-12-25 14:54:44 -0800676.. index::
677 single: parameter; call semantics
678
Georg Brandl116aa622007-08-15 14:28:22 +0000679The primary must evaluate to a callable object (user-defined functions, built-in
680functions, methods of built-in objects, class objects, methods of class
Georg Brandl96593ed2007-09-07 14:15:41 +0000681instances, and all objects having a :meth:`__call__` method are callable). All
682argument expressions are evaluated before the call is attempted. Please refer
Chris Jerdonekb4309942012-12-25 14:54:44 -0800683to section :ref:`function` for the syntax of formal :term:`parameter` lists.
Georg Brandl96593ed2007-09-07 14:15:41 +0000684
685.. XXX update with kwonly args PEP
Georg Brandl116aa622007-08-15 14:28:22 +0000686
687If keyword arguments are present, they are first converted to positional
688arguments, as follows. First, a list of unfilled slots is created for the
689formal parameters. If there are N positional arguments, they are placed in the
690first N slots. Next, for each keyword argument, the identifier is used to
691determine the corresponding slot (if the identifier is the same as the first
692formal parameter name, the first slot is used, and so on). If the slot is
693already filled, a :exc:`TypeError` exception is raised. Otherwise, the value of
694the argument is placed in the slot, filling it (even if the expression is
695``None``, it fills the slot). When all arguments have been processed, the slots
696that are still unfilled are filled with the corresponding default value from the
697function definition. (Default values are calculated, once, when the function is
698defined; thus, a mutable object such as a list or dictionary used as default
699value will be shared by all calls that don't specify an argument value for the
700corresponding slot; this should usually be avoided.) If there are any unfilled
701slots for which no default value is specified, a :exc:`TypeError` exception is
702raised. Otherwise, the list of filled slots is used as the argument list for
703the call.
704
Georg Brandl495f7b52009-10-27 15:28:25 +0000705.. impl-detail::
Georg Brandl48310cd2009-01-03 21:18:54 +0000706
Georg Brandl495f7b52009-10-27 15:28:25 +0000707 An implementation may provide built-in functions whose positional parameters
708 do not have names, even if they are 'named' for the purpose of documentation,
709 and which therefore cannot be supplied by keyword. In CPython, this is the
Georg Brandl60203b42010-10-06 10:11:56 +0000710 case for functions implemented in C that use :c:func:`PyArg_ParseTuple` to
Georg Brandl495f7b52009-10-27 15:28:25 +0000711 parse their arguments.
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000712
Georg Brandl116aa622007-08-15 14:28:22 +0000713If there are more positional arguments than there are formal parameter slots, a
714:exc:`TypeError` exception is raised, unless a formal parameter using the syntax
715``*identifier`` is present; in this case, that formal parameter receives a tuple
716containing the excess positional arguments (or an empty tuple if there were no
717excess positional arguments).
718
719If any keyword argument does not correspond to a formal parameter name, a
720:exc:`TypeError` exception is raised, unless a formal parameter using the syntax
721``**identifier`` is present; in this case, that formal parameter receives a
722dictionary containing the excess keyword arguments (using the keywords as keys
723and the argument values as corresponding values), or a (new) empty dictionary if
724there were no excess keyword arguments.
725
Eli Bendersky7bd081c2011-07-30 07:05:16 +0300726.. index::
727 single: *; in function calls
Martin Panter0c0da482016-06-12 01:46:50 +0000728 single: unpacking; in function calls
Eli Bendersky7bd081c2011-07-30 07:05:16 +0300729
Georg Brandl116aa622007-08-15 14:28:22 +0000730If the syntax ``*expression`` appears in the function call, ``expression`` must
Martin Panter0c0da482016-06-12 01:46:50 +0000731evaluate to an :term:`iterable`. Elements from these iterables are
732treated as if they were additional positional arguments. For the call
733``f(x1, x2, *y, x3, x4)``, if *y* evaluates to a sequence *y1*, ..., *yM*,
734this is equivalent to a call with M+4 positional arguments *x1*, *x2*,
735*y1*, ..., *yM*, *x3*, *x4*.
Georg Brandl116aa622007-08-15 14:28:22 +0000736
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000737A consequence of this is that although the ``*expression`` syntax may appear
Martin Panter0c0da482016-06-12 01:46:50 +0000738*after* explicit keyword arguments, it is processed *before* the
739keyword arguments (and any ``**expression`` arguments -- see below). So::
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741 >>> def f(a, b):
Serhiy Storchakadba90392016-05-10 12:01:23 +0300742 ... print(a, b)
Georg Brandl116aa622007-08-15 14:28:22 +0000743 ...
744 >>> f(b=1, *(2,))
745 2 1
746 >>> f(a=1, *(2,))
747 Traceback (most recent call last):
748 File "<stdin>", line 1, in ?
749 TypeError: f() got multiple values for keyword argument 'a'
750 >>> f(1, *(2,))
751 1 2
752
753It is unusual for both keyword arguments and the ``*expression`` syntax to be
754used in the same call, so in practice this confusion does not arise.
755
Eli Bendersky7bd081c2011-07-30 07:05:16 +0300756.. index::
757 single: **; in function calls
758
Georg Brandl116aa622007-08-15 14:28:22 +0000759If the syntax ``**expression`` appears in the function call, ``expression`` must
Martin Panter0c0da482016-06-12 01:46:50 +0000760evaluate to a :term:`mapping`, the contents of which are treated as
761additional keyword arguments. If a keyword is already present
762(as an explicit keyword argument, or from another unpacking),
763a :exc:`TypeError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +0000764
765Formal parameters using the syntax ``*identifier`` or ``**identifier`` cannot be
766used as positional argument slots or as keyword argument names.
767
Martin Panter0c0da482016-06-12 01:46:50 +0000768.. versionchanged:: 3.5
769 Function calls accept any number of ``*`` and ``**`` unpackings,
770 positional arguments may follow iterable unpackings (``*``),
771 and keyword arguments may follow dictionary unpackings (``**``).
772 Originally proposed by :pep:`448`.
773
Georg Brandl116aa622007-08-15 14:28:22 +0000774A call always returns some value, possibly ``None``, unless it raises an
775exception. How this value is computed depends on the type of the callable
776object.
777
778If it is---
779
780a user-defined function:
781 .. index::
782 pair: function; call
783 triple: user-defined; function; call
784 object: user-defined function
785 object: function
786
787 The code block for the function is executed, passing it the argument list. The
788 first thing the code block will do is bind the formal parameters to the
789 arguments; this is described in section :ref:`function`. When the code block
790 executes a :keyword:`return` statement, this specifies the return value of the
791 function call.
792
793a built-in function or method:
794 .. index::
795 pair: function; call
796 pair: built-in function; call
797 pair: method; call
798 pair: built-in method; call
799 object: built-in method
800 object: built-in function
801 object: method
802 object: function
803
804 The result is up to the interpreter; see :ref:`built-in-funcs` for the
805 descriptions of built-in functions and methods.
806
807a class object:
808 .. index::
809 object: class
810 pair: class object; call
811
812 A new instance of that class is returned.
813
814a class instance method:
815 .. index::
816 object: class instance
817 object: instance
818 pair: class instance; call
819
820 The corresponding user-defined function is called, with an argument list that is
821 one longer than the argument list of the call: the instance becomes the first
822 argument.
823
824a class instance:
825 .. index::
826 pair: instance; call
827 single: __call__() (object method)
828
829 The class must define a :meth:`__call__` method; the effect is then the same as
830 if that method was called.
831
832
Yury Selivanovf3e40fa2015-05-21 11:50:30 -0400833.. _await:
834
835Await expression
836================
837
838Suspend the execution of :term:`coroutine` on an :term:`awaitable` object.
839Can only be used inside a :term:`coroutine function`.
840
841.. productionlist::
Serhiy Storchakac7cc9852016-05-08 21:59:46 +0300842 await_expr: "await" `primary`
Yury Selivanovf3e40fa2015-05-21 11:50:30 -0400843
844.. versionadded:: 3.5
845
846
Georg Brandl116aa622007-08-15 14:28:22 +0000847.. _power:
848
849The power operator
850==================
851
852The power operator binds more tightly than unary operators on its left; it binds
853less tightly than unary operators on its right. The syntax is:
854
855.. productionlist::
Serhiy Storchakac7cc9852016-05-08 21:59:46 +0300856 power: ( `await_expr` | `primary` ) ["**" `u_expr`]
Georg Brandl116aa622007-08-15 14:28:22 +0000857
858Thus, in an unparenthesized sequence of power and unary operators, the operators
859are evaluated from right to left (this does not constrain the evaluation order
Guido van Rossum04110fb2007-08-24 16:32:05 +0000860for the operands): ``-1**2`` results in ``-1``.
Georg Brandl116aa622007-08-15 14:28:22 +0000861
862The power operator has the same semantics as the built-in :func:`pow` function,
863when called with two arguments: it yields its left argument raised to the power
864of its right argument. The numeric arguments are first converted to a common
Georg Brandl96593ed2007-09-07 14:15:41 +0000865type, and the result is of that type.
Georg Brandl116aa622007-08-15 14:28:22 +0000866
Georg Brandl96593ed2007-09-07 14:15:41 +0000867For int operands, the result has the same type as the operands unless the second
868argument is negative; in that case, all arguments are converted to float and a
869float result is delivered. For example, ``10**2`` returns ``100``, but
870``10**-2`` returns ``0.01``.
Georg Brandl116aa622007-08-15 14:28:22 +0000871
872Raising ``0.0`` to a negative power results in a :exc:`ZeroDivisionError`.
Christian Heimes072c0f12008-01-03 23:01:04 +0000873Raising a negative number to a fractional power results in a :class:`complex`
Christian Heimesfaf2f632008-01-06 16:59:19 +0000874number. (In earlier versions it raised a :exc:`ValueError`.)
Georg Brandl116aa622007-08-15 14:28:22 +0000875
876
877.. _unary:
878
Benjamin Petersonba01dd92009-02-20 04:02:38 +0000879Unary arithmetic and bitwise operations
880=======================================
Georg Brandl116aa622007-08-15 14:28:22 +0000881
882.. index::
883 triple: unary; arithmetic; operation
Christian Heimesfaf2f632008-01-06 16:59:19 +0000884 triple: unary; bitwise; operation
Georg Brandl116aa622007-08-15 14:28:22 +0000885
Benjamin Petersonba01dd92009-02-20 04:02:38 +0000886All unary arithmetic and bitwise operations have the same priority:
Georg Brandl116aa622007-08-15 14:28:22 +0000887
888.. productionlist::
889 u_expr: `power` | "-" `u_expr` | "+" `u_expr` | "~" `u_expr`
890
891.. index::
892 single: negation
893 single: minus
894
895The unary ``-`` (minus) operator yields the negation of its numeric argument.
896
897.. index:: single: plus
898
899The unary ``+`` (plus) operator yields its numeric argument unchanged.
900
901.. index:: single: inversion
902
Christian Heimesfaf2f632008-01-06 16:59:19 +0000903
Georg Brandl95817b32008-05-11 14:30:18 +0000904The unary ``~`` (invert) operator yields the bitwise inversion of its integer
905argument. The bitwise inversion of ``x`` is defined as ``-(x+1)``. It only
906applies to integral numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000907
908.. index:: exception: TypeError
909
910In all three cases, if the argument does not have the proper type, a
911:exc:`TypeError` exception is raised.
912
913
914.. _binary:
915
916Binary arithmetic operations
917============================
918
919.. index:: triple: binary; arithmetic; operation
920
921The binary arithmetic operations have the conventional priority levels. Note
922that some of these operations also apply to certain non-numeric types. Apart
923from the power operator, there are only two levels, one for multiplicative
924operators and one for additive operators:
925
926.. productionlist::
Benjamin Petersond51374e2014-04-09 23:55:56 -0400927 m_expr: `u_expr` | `m_expr` "*" `u_expr` | `m_expr` "@" `m_expr` |
928 : `m_expr` "//" `u_expr`| `m_expr` "/" `u_expr` |
929 : `m_expr` "%" `u_expr`
Georg Brandl116aa622007-08-15 14:28:22 +0000930 a_expr: `m_expr` | `a_expr` "+" `m_expr` | `a_expr` "-" `m_expr`
931
932.. index:: single: multiplication
933
934The ``*`` (multiplication) operator yields the product of its arguments. The
Georg Brandl96593ed2007-09-07 14:15:41 +0000935arguments must either both be numbers, or one argument must be an integer and
936the other must be a sequence. In the former case, the numbers are converted to a
937common type and then multiplied together. In the latter case, sequence
938repetition is performed; a negative repetition factor yields an empty sequence.
Georg Brandl116aa622007-08-15 14:28:22 +0000939
Benjamin Petersond51374e2014-04-09 23:55:56 -0400940.. index:: single: matrix multiplication
941
942The ``@`` (at) operator is intended to be used for matrix multiplication. No
943builtin Python types implement this operator.
944
945.. versionadded:: 3.5
946
Georg Brandl116aa622007-08-15 14:28:22 +0000947.. index::
948 exception: ZeroDivisionError
949 single: division
950
951The ``/`` (division) and ``//`` (floor division) operators yield the quotient of
952their arguments. The numeric arguments are first converted to a common type.
Georg Brandl0aaae262013-10-08 21:47:18 +0200953Division of integers yields a float, while floor division of integers results in an
Georg Brandl96593ed2007-09-07 14:15:41 +0000954integer; the result is that of mathematical division with the 'floor' function
955applied to the result. Division by zero raises the :exc:`ZeroDivisionError`
956exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000957
958.. index:: single: modulo
959
960The ``%`` (modulo) operator yields the remainder from the division of the first
961argument by the second. The numeric arguments are first converted to a common
962type. A zero right argument raises the :exc:`ZeroDivisionError` exception. The
963arguments may be floating point numbers, e.g., ``3.14%0.7`` equals ``0.34``
964(since ``3.14`` equals ``4*0.7 + 0.34``.) The modulo operator always yields a
965result with the same sign as its second operand (or zero); the absolute value of
966the result is strictly smaller than the absolute value of the second operand
967[#]_.
968
Georg Brandl96593ed2007-09-07 14:15:41 +0000969The floor division and modulo operators are connected by the following
970identity: ``x == (x//y)*y + (x%y)``. Floor division and modulo are also
971connected with the built-in function :func:`divmod`: ``divmod(x, y) == (x//y,
972x%y)``. [#]_.
Georg Brandl116aa622007-08-15 14:28:22 +0000973
974In addition to performing the modulo operation on numbers, the ``%`` operator is
Georg Brandl96593ed2007-09-07 14:15:41 +0000975also overloaded by string objects to perform old-style string formatting (also
976known as interpolation). The syntax for string formatting is described in the
Georg Brandl4b491312007-08-31 09:22:56 +0000977Python Library Reference, section :ref:`old-string-formatting`.
Georg Brandl116aa622007-08-15 14:28:22 +0000978
979The floor division operator, the modulo operator, and the :func:`divmod`
Georg Brandl96593ed2007-09-07 14:15:41 +0000980function are not defined for complex numbers. Instead, convert to a floating
981point number using the :func:`abs` function if appropriate.
Georg Brandl116aa622007-08-15 14:28:22 +0000982
983.. index:: single: addition
984
Georg Brandl96593ed2007-09-07 14:15:41 +0000985The ``+`` (addition) operator yields the sum of its arguments. The arguments
Raymond Hettingeraa7886d2014-05-26 22:20:37 -0700986must either both be numbers or both be sequences of the same type. In the
987former case, the numbers are converted to a common type and then added together.
988In the latter case, the sequences are concatenated.
Georg Brandl116aa622007-08-15 14:28:22 +0000989
990.. index:: single: subtraction
991
992The ``-`` (subtraction) operator yields the difference of its arguments. The
993numeric arguments are first converted to a common type.
994
995
996.. _shifting:
997
998Shifting operations
999===================
1000
1001.. index:: pair: shifting; operation
1002
1003The shifting operations have lower priority than the arithmetic operations:
1004
1005.. productionlist::
1006 shift_expr: `a_expr` | `shift_expr` ( "<<" | ">>" ) `a_expr`
1007
Georg Brandl96593ed2007-09-07 14:15:41 +00001008These operators accept integers as arguments. They shift the first argument to
1009the left or right by the number of bits given by the second argument.
Georg Brandl116aa622007-08-15 14:28:22 +00001010
1011.. index:: exception: ValueError
1012
Georg Brandl0aaae262013-10-08 21:47:18 +02001013A right shift by *n* bits is defined as floor division by ``pow(2,n)``. A left
1014shift by *n* bits is defined as multiplication with ``pow(2,n)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001015
Benjamin Peterson08bf91c2010-04-11 16:12:57 +00001016.. note::
1017
1018 In the current implementation, the right-hand operand is required
Mark Dickinson505add32010-04-06 18:22:06 +00001019 to be at most :attr:`sys.maxsize`. If the right-hand operand is larger than
1020 :attr:`sys.maxsize` an :exc:`OverflowError` exception is raised.
Georg Brandl116aa622007-08-15 14:28:22 +00001021
1022.. _bitwise:
1023
Christian Heimesfaf2f632008-01-06 16:59:19 +00001024Binary bitwise operations
1025=========================
Georg Brandl116aa622007-08-15 14:28:22 +00001026
Christian Heimesfaf2f632008-01-06 16:59:19 +00001027.. index:: triple: binary; bitwise; operation
Georg Brandl116aa622007-08-15 14:28:22 +00001028
1029Each of the three bitwise operations has a different priority level:
1030
1031.. productionlist::
1032 and_expr: `shift_expr` | `and_expr` "&" `shift_expr`
1033 xor_expr: `and_expr` | `xor_expr` "^" `and_expr`
1034 or_expr: `xor_expr` | `or_expr` "|" `xor_expr`
1035
Christian Heimesfaf2f632008-01-06 16:59:19 +00001036.. index:: pair: bitwise; and
Georg Brandl116aa622007-08-15 14:28:22 +00001037
Georg Brandl96593ed2007-09-07 14:15:41 +00001038The ``&`` operator yields the bitwise AND of its arguments, which must be
1039integers.
Georg Brandl116aa622007-08-15 14:28:22 +00001040
1041.. index::
Christian Heimesfaf2f632008-01-06 16:59:19 +00001042 pair: bitwise; xor
Georg Brandl116aa622007-08-15 14:28:22 +00001043 pair: exclusive; or
1044
1045The ``^`` operator yields the bitwise XOR (exclusive OR) of its arguments, which
Georg Brandl96593ed2007-09-07 14:15:41 +00001046must be integers.
Georg Brandl116aa622007-08-15 14:28:22 +00001047
1048.. index::
Christian Heimesfaf2f632008-01-06 16:59:19 +00001049 pair: bitwise; or
Georg Brandl116aa622007-08-15 14:28:22 +00001050 pair: inclusive; or
1051
1052The ``|`` operator yields the bitwise (inclusive) OR of its arguments, which
Georg Brandl96593ed2007-09-07 14:15:41 +00001053must be integers.
Georg Brandl116aa622007-08-15 14:28:22 +00001054
1055
1056.. _comparisons:
1057
1058Comparisons
1059===========
1060
1061.. index:: single: comparison
1062
1063.. index:: pair: C; language
1064
1065Unlike C, all comparison operations in Python have the same priority, which is
1066lower than that of any arithmetic, shifting or bitwise operation. Also unlike
1067C, expressions like ``a < b < c`` have the interpretation that is conventional
1068in mathematics:
1069
1070.. productionlist::
1071 comparison: `or_expr` ( `comp_operator` `or_expr` )*
1072 comp_operator: "<" | ">" | "==" | ">=" | "<=" | "!="
1073 : | "is" ["not"] | ["not"] "in"
1074
1075Comparisons yield boolean values: ``True`` or ``False``.
1076
1077.. index:: pair: chaining; comparisons
1078
1079Comparisons can be chained arbitrarily, e.g., ``x < y <= z`` is equivalent to
1080``x < y and y <= z``, except that ``y`` is evaluated only once (but in both
1081cases ``z`` is not evaluated at all when ``x < y`` is found to be false).
1082
Guido van Rossum04110fb2007-08-24 16:32:05 +00001083Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and *op1*, *op2*, ...,
1084*opN* are comparison operators, then ``a op1 b op2 c ... y opN z`` is equivalent
1085to ``a op1 b and b op2 c and ... y opN z``, except that each expression is
1086evaluated at most once.
Georg Brandl116aa622007-08-15 14:28:22 +00001087
Guido van Rossum04110fb2007-08-24 16:32:05 +00001088Note that ``a op1 b op2 c`` doesn't imply any kind of comparison between *a* and
Georg Brandl116aa622007-08-15 14:28:22 +00001089*c*, so that, e.g., ``x < y > z`` is perfectly legal (though perhaps not
1090pretty).
1091
Martin Panteraa0da862015-09-23 05:28:13 +00001092Value comparisons
1093-----------------
1094
Georg Brandl116aa622007-08-15 14:28:22 +00001095The operators ``<``, ``>``, ``==``, ``>=``, ``<=``, and ``!=`` compare the
Martin Panteraa0da862015-09-23 05:28:13 +00001096values of two objects. The objects do not need to have the same type.
Georg Brandl116aa622007-08-15 14:28:22 +00001097
Martin Panteraa0da862015-09-23 05:28:13 +00001098Chapter :ref:`objects` states that objects have a value (in addition to type
1099and identity). The value of an object is a rather abstract notion in Python:
1100For example, there is no canonical access method for an object's value. Also,
1101there is no requirement that the value of an object should be constructed in a
1102particular way, e.g. comprised of all its data attributes. Comparison operators
1103implement a particular notion of what the value of an object is. One can think
1104of them as defining the value of an object indirectly, by means of their
1105comparison implementation.
Georg Brandl116aa622007-08-15 14:28:22 +00001106
Martin Panteraa0da862015-09-23 05:28:13 +00001107Because all types are (direct or indirect) subtypes of :class:`object`, they
1108inherit the default comparison behavior from :class:`object`. Types can
1109customize their comparison behavior by implementing
1110:dfn:`rich comparison methods` like :meth:`__lt__`, described in
1111:ref:`customization`.
Georg Brandl116aa622007-08-15 14:28:22 +00001112
Martin Panteraa0da862015-09-23 05:28:13 +00001113The default behavior for equality comparison (``==`` and ``!=``) is based on
1114the identity of the objects. Hence, equality comparison of instances with the
1115same identity results in equality, and equality comparison of instances with
1116different identities results in inequality. A motivation for this default
1117behavior is the desire that all objects should be reflexive (i.e. ``x is y``
1118implies ``x == y``).
1119
1120A default order comparison (``<``, ``>``, ``<=``, and ``>=``) is not provided;
1121an attempt raises :exc:`TypeError`. A motivation for this default behavior is
1122the lack of a similar invariant as for equality.
1123
1124The behavior of the default equality comparison, that instances with different
1125identities are always unequal, may be in contrast to what types will need that
1126have a sensible definition of object value and value-based equality. Such
1127types will need to customize their comparison behavior, and in fact, a number
1128of built-in types have done that.
1129
1130The following list describes the comparison behavior of the most important
1131built-in types.
1132
1133* Numbers of built-in numeric types (:ref:`typesnumeric`) and of the standard
1134 library types :class:`fractions.Fraction` and :class:`decimal.Decimal` can be
1135 compared within and across their types, with the restriction that complex
1136 numbers do not support order comparison. Within the limits of the types
1137 involved, they compare mathematically (algorithmically) correct without loss
1138 of precision.
1139
1140 The not-a-number values :const:`float('NaN')` and :const:`Decimal('NaN')`
1141 are special. They are identical to themselves (``x is x`` is true) but
1142 are not equal to themselves (``x == x`` is false). Additionally,
1143 comparing any number to a not-a-number value
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001144 will return ``False``. For example, both ``3 < float('NaN')`` and
1145 ``float('NaN') < 3`` will return ``False``.
1146
Martin Panteraa0da862015-09-23 05:28:13 +00001147* Binary sequences (instances of :class:`bytes` or :class:`bytearray`) can be
1148 compared within and across their types. They compare lexicographically using
1149 the numeric values of their elements.
Georg Brandl4b491312007-08-31 09:22:56 +00001150
Martin Panteraa0da862015-09-23 05:28:13 +00001151* Strings (instances of :class:`str`) compare lexicographically using the
1152 numerical Unicode code points (the result of the built-in function
1153 :func:`ord`) of their characters. [#]_
Georg Brandl116aa622007-08-15 14:28:22 +00001154
Martin Panteraa0da862015-09-23 05:28:13 +00001155 Strings and binary sequences cannot be directly compared.
Georg Brandl116aa622007-08-15 14:28:22 +00001156
Martin Panteraa0da862015-09-23 05:28:13 +00001157* Sequences (instances of :class:`tuple`, :class:`list`, or :class:`range`) can
1158 be compared only within each of their types, with the restriction that ranges
1159 do not support order comparison. Equality comparison across these types
1160 results in unequality, and ordering comparison across these types raises
1161 :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +00001162
Martin Panteraa0da862015-09-23 05:28:13 +00001163 Sequences compare lexicographically using comparison of corresponding
1164 elements, whereby reflexivity of the elements is enforced.
Georg Brandl116aa622007-08-15 14:28:22 +00001165
Martin Panteraa0da862015-09-23 05:28:13 +00001166 In enforcing reflexivity of elements, the comparison of collections assumes
1167 that for a collection element ``x``, ``x == x`` is always true. Based on
1168 that assumption, element identity is compared first, and element comparison
1169 is performed only for distinct elements. This approach yields the same
1170 result as a strict element comparison would, if the compared elements are
1171 reflexive. For non-reflexive elements, the result is different than for
1172 strict element comparison, and may be surprising: The non-reflexive
1173 not-a-number values for example result in the following comparison behavior
1174 when used in a list::
1175
1176 >>> nan = float('NaN')
1177 >>> nan is nan
1178 True
1179 >>> nan == nan
1180 False <-- the defined non-reflexive behavior of NaN
1181 >>> [nan] == [nan]
1182 True <-- list enforces reflexivity and tests identity first
1183
1184 Lexicographical comparison between built-in collections works as follows:
1185
1186 - For two collections to compare equal, they must be of the same type, have
1187 the same length, and each pair of corresponding elements must compare
1188 equal (for example, ``[1,2] == (1,2)`` is false because the type is not the
1189 same).
1190
1191 - Collections that support order comparison are ordered the same as their
1192 first unequal elements (for example, ``[1,2,x] <= [1,2,y]`` has the same
1193 value as ``x <= y``). If a corresponding element does not exist, the
1194 shorter collection is ordered first (for example, ``[1,2] < [1,2,3]`` is
1195 true).
1196
1197* Mappings (instances of :class:`dict`) compare equal if and only if they have
1198 equal `(key, value)` pairs. Equality comparison of the keys and elements
1199 enforces reflexivity.
1200
1201 Order comparisons (``<``, ``>``, ``<=``, and ``>=``) raise :exc:`TypeError`.
1202
1203* Sets (instances of :class:`set` or :class:`frozenset`) can be compared within
1204 and across their types.
1205
1206 They define order
1207 comparison operators to mean subset and superset tests. Those relations do
1208 not define total orderings (for example, the two sets ``{1,2}`` and ``{2,3}``
1209 are not equal, nor subsets of one another, nor supersets of one
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001210 another). Accordingly, sets are not appropriate arguments for functions
Martin Panteraa0da862015-09-23 05:28:13 +00001211 which depend on total ordering (for example, :func:`min`, :func:`max`, and
1212 :func:`sorted` produce undefined results given a list of sets as inputs).
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001213
Martin Panteraa0da862015-09-23 05:28:13 +00001214 Comparison of sets enforces reflexivity of its elements.
Georg Brandl116aa622007-08-15 14:28:22 +00001215
Martin Panteraa0da862015-09-23 05:28:13 +00001216* Most other built-in types have no comparison methods implemented, so they
1217 inherit the default comparison behavior.
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001218
Martin Panteraa0da862015-09-23 05:28:13 +00001219User-defined classes that customize their comparison behavior should follow
1220some consistency rules, if possible:
1221
1222* Equality comparison should be reflexive.
1223 In other words, identical objects should compare equal:
1224
1225 ``x is y`` implies ``x == y``
1226
1227* Comparison should be symmetric.
1228 In other words, the following expressions should have the same result:
1229
1230 ``x == y`` and ``y == x``
1231
1232 ``x != y`` and ``y != x``
1233
1234 ``x < y`` and ``y > x``
1235
1236 ``x <= y`` and ``y >= x``
1237
1238* Comparison should be transitive.
1239 The following (non-exhaustive) examples illustrate that:
1240
1241 ``x > y and y > z`` implies ``x > z``
1242
1243 ``x < y and y <= z`` implies ``x < z``
1244
1245* Inverse comparison should result in the boolean negation.
1246 In other words, the following expressions should have the same result:
1247
1248 ``x == y`` and ``not x != y``
1249
1250 ``x < y`` and ``not x >= y`` (for total ordering)
1251
1252 ``x > y`` and ``not x <= y`` (for total ordering)
1253
1254 The last two expressions apply to totally ordered collections (e.g. to
1255 sequences, but not to sets or mappings). See also the
1256 :func:`~functools.total_ordering` decorator.
1257
1258Python does not enforce these consistency rules. In fact, the not-a-number
1259values are an example for not following these rules.
1260
1261
1262.. _in:
1263.. _not in:
Georg Brandl495f7b52009-10-27 15:28:25 +00001264.. _membership-test-details:
1265
Martin Panteraa0da862015-09-23 05:28:13 +00001266Membership test operations
1267--------------------------
1268
Georg Brandl96593ed2007-09-07 14:15:41 +00001269The operators :keyword:`in` and :keyword:`not in` test for membership. ``x in
1270s`` evaluates to true if *x* is a member of *s*, and false otherwise. ``x not
1271in s`` returns the negation of ``x in s``. All built-in sequences and set types
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001272support this as well as dictionary, for which :keyword:`in` tests whether the
Raymond Hettingera2a08fb2008-11-17 22:55:16 +00001273dictionary has a given key. For container types such as list, tuple, set,
Raymond Hettinger0cc818f2008-11-21 10:40:51 +00001274frozenset, dict, or collections.deque, the expression ``x in y`` is equivalent
Stefan Krahc8bdc012010-04-01 10:34:09 +00001275to ``any(x is e or x == e for e in y)``.
Georg Brandl116aa622007-08-15 14:28:22 +00001276
Georg Brandl4b491312007-08-31 09:22:56 +00001277For the string and bytes types, ``x in y`` is true if and only if *x* is a
1278substring of *y*. An equivalent test is ``y.find(x) != -1``. Empty strings are
1279always considered to be a substring of any other string, so ``"" in "abc"`` will
1280return ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +00001281
Georg Brandl116aa622007-08-15 14:28:22 +00001282For user-defined classes which define the :meth:`__contains__` method, ``x in
1283y`` is true if and only if ``y.__contains__(x)`` is true.
1284
Georg Brandl495f7b52009-10-27 15:28:25 +00001285For user-defined classes which do not define :meth:`__contains__` but do define
1286:meth:`__iter__`, ``x in y`` is true if some value ``z`` with ``x == z`` is
1287produced while iterating over ``y``. If an exception is raised during the
1288iteration, it is as if :keyword:`in` raised that exception.
1289
1290Lastly, the old-style iteration protocol is tried: if a class defines
Georg Brandl116aa622007-08-15 14:28:22 +00001291:meth:`__getitem__`, ``x in y`` is true if and only if there is a non-negative
1292integer index *i* such that ``x == y[i]``, and all lower integer indices do not
Georg Brandl96593ed2007-09-07 14:15:41 +00001293raise :exc:`IndexError` exception. (If any other exception is raised, it is as
Georg Brandl116aa622007-08-15 14:28:22 +00001294if :keyword:`in` raised that exception).
1295
1296.. index::
1297 operator: in
1298 operator: not in
1299 pair: membership; test
1300 object: sequence
1301
1302The operator :keyword:`not in` is defined to have the inverse true value of
1303:keyword:`in`.
1304
1305.. index::
1306 operator: is
1307 operator: is not
1308 pair: identity; test
1309
Martin Panteraa0da862015-09-23 05:28:13 +00001310
1311.. _is:
1312.. _is not:
1313
1314Identity comparisons
1315--------------------
1316
Georg Brandl116aa622007-08-15 14:28:22 +00001317The operators :keyword:`is` and :keyword:`is not` test for object identity: ``x
1318is y`` is true if and only if *x* and *y* are the same object. ``x is not y``
Benjamin Peterson41181742008-07-02 20:22:54 +00001319yields the inverse truth value. [#]_
Georg Brandl116aa622007-08-15 14:28:22 +00001320
1321
1322.. _booleans:
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001323.. _and:
1324.. _or:
1325.. _not:
Georg Brandl116aa622007-08-15 14:28:22 +00001326
1327Boolean operations
1328==================
1329
1330.. index::
1331 pair: Conditional; expression
1332 pair: Boolean; operation
1333
Georg Brandl116aa622007-08-15 14:28:22 +00001334.. productionlist::
Georg Brandl116aa622007-08-15 14:28:22 +00001335 or_test: `and_test` | `or_test` "or" `and_test`
1336 and_test: `not_test` | `and_test` "and" `not_test`
1337 not_test: `comparison` | "not" `not_test`
1338
1339In the context of Boolean operations, and also when expressions are used by
1340control flow statements, the following values are interpreted as false:
1341``False``, ``None``, numeric zero of all types, and empty strings and containers
1342(including strings, tuples, lists, dictionaries, sets and frozensets). All
Georg Brandl96593ed2007-09-07 14:15:41 +00001343other values are interpreted as true. User-defined objects can customize their
1344truth value by providing a :meth:`__bool__` method.
Georg Brandl116aa622007-08-15 14:28:22 +00001345
1346.. index:: operator: not
1347
1348The operator :keyword:`not` yields ``True`` if its argument is false, ``False``
1349otherwise.
1350
Georg Brandl116aa622007-08-15 14:28:22 +00001351.. index:: operator: and
1352
1353The expression ``x and y`` first evaluates *x*; if *x* is false, its value is
1354returned; otherwise, *y* is evaluated and the resulting value is returned.
1355
1356.. index:: operator: or
1357
1358The expression ``x or y`` first evaluates *x*; if *x* is true, its value is
1359returned; otherwise, *y* is evaluated and the resulting value is returned.
1360
1361(Note that neither :keyword:`and` nor :keyword:`or` restrict the value and type
1362they return to ``False`` and ``True``, but rather return the last evaluated
Georg Brandl96593ed2007-09-07 14:15:41 +00001363argument. This is sometimes useful, e.g., if ``s`` is a string that should be
Georg Brandl116aa622007-08-15 14:28:22 +00001364replaced by a default value if it is empty, the expression ``s or 'foo'`` yields
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001365the desired value. Because :keyword:`not` has to create a new value, it
1366returns a boolean value regardless of the type of its argument
1367(for example, ``not 'foo'`` produces ``False`` rather than ``''``.)
Georg Brandl116aa622007-08-15 14:28:22 +00001368
1369
Alexander Belopolsky50ba19e2010-12-15 19:47:37 +00001370Conditional expressions
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001371=======================
1372
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001373.. index::
1374 pair: conditional; expression
1375 pair: ternary; operator
1376
1377.. productionlist::
1378 conditional_expression: `or_test` ["if" `or_test` "else" `expression`]
Georg Brandl242e6a02013-10-06 10:28:39 +02001379 expression: `conditional_expression` | `lambda_expr`
1380 expression_nocond: `or_test` | `lambda_expr_nocond`
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001381
1382Conditional expressions (sometimes called a "ternary operator") have the lowest
1383priority of all Python operations.
1384
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001385The expression ``x if C else y`` first evaluates the condition, *C* rather than *x*.
1386If *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001387evaluated and its value is returned.
1388
1389See :pep:`308` for more details about conditional expressions.
1390
1391
Georg Brandl116aa622007-08-15 14:28:22 +00001392.. _lambdas:
Georg Brandlc4f8b242009-04-10 08:17:21 +00001393.. _lambda:
Georg Brandl116aa622007-08-15 14:28:22 +00001394
1395Lambdas
1396=======
1397
1398.. index::
1399 pair: lambda; expression
1400 pair: lambda; form
1401 pair: anonymous; function
1402
1403.. productionlist::
Georg Brandl242e6a02013-10-06 10:28:39 +02001404 lambda_expr: "lambda" [`parameter_list`]: `expression`
1405 lambda_expr_nocond: "lambda" [`parameter_list`]: `expression_nocond`
Georg Brandl116aa622007-08-15 14:28:22 +00001406
Zachary Ware2f78b842014-06-03 09:32:40 -05001407Lambda expressions (sometimes called lambda forms) are used to create anonymous
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001408functions. The expression ``lambda arguments: expression`` yields a function
Martin Panter1050d2d2016-07-26 11:18:21 +02001409object. The unnamed object behaves like a function object defined with:
1410
1411.. code-block:: none
Georg Brandl116aa622007-08-15 14:28:22 +00001412
Georg Brandl96593ed2007-09-07 14:15:41 +00001413 def <lambda>(arguments):
Georg Brandl116aa622007-08-15 14:28:22 +00001414 return expression
1415
1416See section :ref:`function` for the syntax of parameter lists. Note that
Georg Brandl242e6a02013-10-06 10:28:39 +02001417functions created with lambda expressions cannot contain statements or
1418annotations.
Georg Brandl116aa622007-08-15 14:28:22 +00001419
Georg Brandl116aa622007-08-15 14:28:22 +00001420
1421.. _exprlists:
1422
1423Expression lists
1424================
1425
1426.. index:: pair: expression; list
1427
1428.. productionlist::
1429 expression_list: `expression` ( "," `expression` )* [","]
Martin Panter0c0da482016-06-12 01:46:50 +00001430 starred_list: `starred_item` ( "," `starred_item` )* [","]
1431 starred_expression: `expression` | ( `starred_item` "," )* [`starred_item`]
1432 starred_item: `expression` | "*" `or_expr`
Georg Brandl116aa622007-08-15 14:28:22 +00001433
1434.. index:: object: tuple
1435
Martin Panter0c0da482016-06-12 01:46:50 +00001436Except when part of a list or set display, an expression list
1437containing at least one comma yields a tuple. The length of
Georg Brandl116aa622007-08-15 14:28:22 +00001438the tuple is the number of expressions in the list. The expressions are
1439evaluated from left to right.
1440
Martin Panter0c0da482016-06-12 01:46:50 +00001441.. index::
1442 pair: iterable; unpacking
1443 single: *; in expression lists
1444
1445An asterisk ``*`` denotes :dfn:`iterable unpacking`. Its operand must be
1446an :term:`iterable`. The iterable is expanded into a sequence of items,
1447which are included in the new tuple, list, or set, at the site of
1448the unpacking.
1449
1450.. versionadded:: 3.5
1451 Iterable unpacking in expression lists, originally proposed by :pep:`448`.
1452
Georg Brandl116aa622007-08-15 14:28:22 +00001453.. index:: pair: trailing; comma
1454
1455The trailing comma is required only to create a single tuple (a.k.a. a
1456*singleton*); it is optional in all other cases. A single expression without a
1457trailing comma doesn't create a tuple, but rather yields the value of that
1458expression. (To create an empty tuple, use an empty pair of parentheses:
1459``()``.)
1460
1461
1462.. _evalorder:
1463
1464Evaluation order
1465================
1466
1467.. index:: pair: evaluation; order
1468
Georg Brandl96593ed2007-09-07 14:15:41 +00001469Python evaluates expressions from left to right. Notice that while evaluating
1470an assignment, the right-hand side is evaluated before the left-hand side.
Georg Brandl116aa622007-08-15 14:28:22 +00001471
1472In the following lines, expressions will be evaluated in the arithmetic order of
1473their suffixes::
1474
1475 expr1, expr2, expr3, expr4
1476 (expr1, expr2, expr3, expr4)
1477 {expr1: expr2, expr3: expr4}
1478 expr1 + expr2 * (expr3 - expr4)
Georg Brandl734e2682008-08-12 08:18:18 +00001479 expr1(expr2, expr3, *expr4, **expr5)
Georg Brandl116aa622007-08-15 14:28:22 +00001480 expr3, expr4 = expr1, expr2
1481
1482
1483.. _operator-summary:
1484
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001485Operator precedence
1486===================
Georg Brandl116aa622007-08-15 14:28:22 +00001487
1488.. index:: pair: operator; precedence
1489
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001490The following table summarizes the operator precedence in Python, from lowest
Georg Brandl96593ed2007-09-07 14:15:41 +00001491precedence (least binding) to highest precedence (most binding). Operators in
Georg Brandl116aa622007-08-15 14:28:22 +00001492the same box have the same precedence. Unless the syntax is explicitly given,
1493operators are binary. Operators in the same box group left to right (except for
Raymond Hettingeraa7886d2014-05-26 22:20:37 -07001494exponentiation, which groups from right to left).
1495
1496Note that comparisons, membership tests, and identity tests, all have the same
1497precedence and have a left-to-right chaining feature as described in the
1498:ref:`comparisons` section.
Georg Brandl116aa622007-08-15 14:28:22 +00001499
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001500
1501+-----------------------------------------------+-------------------------------------+
1502| Operator | Description |
1503+===============================================+=====================================+
1504| :keyword:`lambda` | Lambda expression |
1505+-----------------------------------------------+-------------------------------------+
Georg Brandl93dc9eb2010-03-14 10:56:14 +00001506| :keyword:`if` -- :keyword:`else` | Conditional expression |
1507+-----------------------------------------------+-------------------------------------+
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001508| :keyword:`or` | Boolean OR |
1509+-----------------------------------------------+-------------------------------------+
1510| :keyword:`and` | Boolean AND |
1511+-----------------------------------------------+-------------------------------------+
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001512| :keyword:`not` ``x`` | Boolean NOT |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001513+-----------------------------------------------+-------------------------------------+
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001514| :keyword:`in`, :keyword:`not in`, | Comparisons, including membership |
Georg Brandl44ea77b2013-03-28 13:28:44 +01001515| :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests |
Georg Brandla5ebc262009-06-03 07:26:22 +00001516| ``<=``, ``>``, ``>=``, ``!=``, ``==`` | |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001517+-----------------------------------------------+-------------------------------------+
1518| ``|`` | Bitwise OR |
1519+-----------------------------------------------+-------------------------------------+
1520| ``^`` | Bitwise XOR |
1521+-----------------------------------------------+-------------------------------------+
1522| ``&`` | Bitwise AND |
1523+-----------------------------------------------+-------------------------------------+
1524| ``<<``, ``>>`` | Shifts |
1525+-----------------------------------------------+-------------------------------------+
1526| ``+``, ``-`` | Addition and subtraction |
1527+-----------------------------------------------+-------------------------------------+
Benjamin Petersond51374e2014-04-09 23:55:56 -04001528| ``*``, ``@``, ``/``, ``//``, ``%`` | Multiplication, matrix |
1529| | multiplication division, |
1530| | remainder [#]_ |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001531+-----------------------------------------------+-------------------------------------+
1532| ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT |
1533+-----------------------------------------------+-------------------------------------+
1534| ``**`` | Exponentiation [#]_ |
1535+-----------------------------------------------+-------------------------------------+
Yury Selivanovf3e40fa2015-05-21 11:50:30 -04001536| ``await`` ``x`` | Await expression |
1537+-----------------------------------------------+-------------------------------------+
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001538| ``x[index]``, ``x[index:index]``, | Subscription, slicing, |
1539| ``x(arguments...)``, ``x.attribute`` | call, attribute reference |
1540+-----------------------------------------------+-------------------------------------+
1541| ``(expressions...)``, | Binding or tuple display, |
1542| ``[expressions...]``, | list display, |
Ezio Melotti9f929bb2012-12-25 15:45:15 +02001543| ``{key: value...}``, | dictionary display, |
Brett Cannon925914f2010-11-21 19:58:24 +00001544| ``{expressions...}`` | set display |
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001545+-----------------------------------------------+-------------------------------------+
1546
Georg Brandl116aa622007-08-15 14:28:22 +00001547
1548.. rubric:: Footnotes
1549
Georg Brandl116aa622007-08-15 14:28:22 +00001550.. [#] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it may not be
1551 true numerically due to roundoff. For example, and assuming a platform on which
1552 a Python float is an IEEE 754 double-precision number, in order that ``-1e-100 %
1553 1e100`` have the same sign as ``1e100``, the computed result is ``-1e-100 +
Georg Brandl063f2372010-12-01 15:32:43 +00001554 1e100``, which is numerically exactly equal to ``1e100``. The function
1555 :func:`math.fmod` returns a result whose sign matches the sign of the
Georg Brandl116aa622007-08-15 14:28:22 +00001556 first argument instead, and so returns ``-1e-100`` in this case. Which approach
1557 is more appropriate depends on the application.
1558
1559.. [#] If x is very close to an exact integer multiple of y, it's possible for
Georg Brandl96593ed2007-09-07 14:15:41 +00001560 ``x//y`` to be one larger than ``(x-x%y)//y`` due to rounding. In such
Georg Brandl116aa622007-08-15 14:28:22 +00001561 cases, Python returns the latter result, in order to preserve that
1562 ``divmod(x,y)[0] * y + x % y`` be very close to ``x``.
1563
Martin Panteraa0da862015-09-23 05:28:13 +00001564.. [#] The Unicode standard distinguishes between :dfn:`code points`
1565 (e.g. U+0041) and :dfn:`abstract characters` (e.g. "LATIN CAPITAL LETTER A").
1566 While most abstract characters in Unicode are only represented using one
1567 code point, there is a number of abstract characters that can in addition be
1568 represented using a sequence of more than one code point. For example, the
1569 abstract character "LATIN CAPITAL LETTER C WITH CEDILLA" can be represented
1570 as a single :dfn:`precomposed character` at code position U+00C7, or as a
1571 sequence of a :dfn:`base character` at code position U+0043 (LATIN CAPITAL
1572 LETTER C), followed by a :dfn:`combining character` at code position U+0327
1573 (COMBINING CEDILLA).
1574
1575 The comparison operators on strings compare at the level of Unicode code
1576 points. This may be counter-intuitive to humans. For example,
1577 ``"\u00C7" == "\u0043\u0327"`` is ``False``, even though both strings
1578 represent the same abstract character "LATIN CAPITAL LETTER C WITH CEDILLA".
1579
1580 To compare strings at the level of abstract characters (that is, in a way
1581 intuitive to humans), use :func:`unicodedata.normalize`.
Guido van Rossumda27fd22007-08-17 00:24:54 +00001582
Georg Brandl48310cd2009-01-03 21:18:54 +00001583.. [#] Due to automatic garbage-collection, free lists, and the dynamic nature of
Benjamin Peterson41181742008-07-02 20:22:54 +00001584 descriptors, you may notice seemingly unusual behaviour in certain uses of
1585 the :keyword:`is` operator, like those involving comparisons between instance
1586 methods, or constants. Check their documentation for more info.
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001587
Georg Brandl063f2372010-12-01 15:32:43 +00001588.. [#] The ``%`` operator is also used for string formatting; the same
1589 precedence applies.
Georg Brandlf1d633c2010-09-20 06:29:01 +00001590
Benjamin Petersonba01dd92009-02-20 04:02:38 +00001591.. [#] The power operator ``**`` binds less tightly than an arithmetic or
1592 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``.