1.. _compound: 2 3******************* 4Compound statements 5******************* 6 7.. index:: pair: compound; statement 8 9Compound statements contain (groups of) other statements; they affect or control 10the execution of those other statements in some way. In general, compound 11statements span multiple lines, although in simple incarnations a whole compound 12statement may be contained in one line. 13 14The :keyword:`if`, :keyword:`while` and :keyword:`for` statements implement 15traditional control flow constructs. :keyword:`try` specifies exception 16handlers and/or cleanup code for a group of statements, while the 17:keyword:`with` statement allows the execution of initialization and 18finalization code around a block of code. Function and class definitions are 19also syntactically compound statements. 20 21.. index:: 22 single: clause 23 single: suite 24 single: ; (semicolon) 25 26A compound statement consists of one or more 'clauses.' A clause consists of a 27header and a 'suite.' The clause headers of a particular compound statement are 28all at the same indentation level. Each clause header begins with a uniquely 29identifying keyword and ends with a colon. A suite is a group of statements 30controlled by a clause. A suite can be one or more semicolon-separated simple 31statements on the same line as the header, following the header's colon, or it 32can be one or more indented statements on subsequent lines. Only the latter 33form of a suite can contain nested compound statements; the following is illegal, 34mostly because it wouldn't be clear to which :keyword:`if` clause a following 35:keyword:`else` clause would belong:: 36 37 if test1: if test2: print(x) 38 39Also note that the semicolon binds tighter than the colon in this context, so 40that in the following example, either all or none of the :func:`print` calls are 41executed:: 42 43 if x < y < z: print(x); print(y); print(z) 44 45Summarizing: 46 47 48.. productionlist:: python-grammar 49 compound_stmt: `if_stmt` 50 : | `while_stmt` 51 : | `for_stmt` 52 : | `try_stmt` 53 : | `with_stmt` 54 : | `funcdef` 55 : | `classdef` 56 : | `async_with_stmt` 57 : | `async_for_stmt` 58 : | `async_funcdef` 59 suite: `stmt_list` NEWLINE | NEWLINE INDENT `statement`+ DEDENT 60 statement: `stmt_list` NEWLINE | `compound_stmt` 61 stmt_list: `simple_stmt` (";" `simple_stmt`)* [";"] 62 63.. index:: 64 single: NEWLINE token 65 single: DEDENT token 66 pair: dangling; else 67 68Note that statements always end in a ``NEWLINE`` possibly followed by a 69``DEDENT``. Also note that optional continuation clauses always begin with a 70keyword that cannot start a statement, thus there are no ambiguities (the 71'dangling :keyword:`else`' problem is solved in Python by requiring nested 72:keyword:`if` statements to be indented). 73 74The formatting of the grammar rules in the following sections places each clause 75on a separate line for clarity. 76 77 78.. _if: 79.. _elif: 80.. _else: 81 82The :keyword:`!if` statement 83============================ 84 85.. index:: 86 ! statement: if 87 keyword: elif 88 keyword: else 89 single: : (colon); compound statement 90 91The :keyword:`if` statement is used for conditional execution: 92 93.. productionlist:: python-grammar 94 if_stmt: "if" `assignment_expression` ":" `suite` 95 : ("elif" `assignment_expression` ":" `suite`)* 96 : ["else" ":" `suite`] 97 98It selects exactly one of the suites by evaluating the expressions one by one 99until one is found to be true (see section :ref:`booleans` for the definition of 100true and false); then that suite is executed (and no other part of the 101:keyword:`if` statement is executed or evaluated). If all expressions are 102false, the suite of the :keyword:`else` clause, if present, is executed. 103 104 105.. _while: 106 107The :keyword:`!while` statement 108=============================== 109 110.. index:: 111 ! statement: while 112 keyword: else 113 pair: loop; statement 114 single: : (colon); compound statement 115 116The :keyword:`while` statement is used for repeated execution as long as an 117expression is true: 118 119.. productionlist:: python-grammar 120 while_stmt: "while" `assignment_expression` ":" `suite` 121 : ["else" ":" `suite`] 122 123This repeatedly tests the expression and, if it is true, executes the first 124suite; if the expression is false (which may be the first time it is tested) the 125suite of the :keyword:`!else` clause, if present, is executed and the loop 126terminates. 127 128.. index:: 129 statement: break 130 statement: continue 131 132A :keyword:`break` statement executed in the first suite terminates the loop 133without executing the :keyword:`!else` clause's suite. A :keyword:`continue` 134statement executed in the first suite skips the rest of the suite and goes back 135to testing the expression. 136 137 138.. _for: 139 140The :keyword:`!for` statement 141============================= 142 143.. index:: 144 ! statement: for 145 keyword: in 146 keyword: else 147 pair: target; list 148 pair: loop; statement 149 object: sequence 150 single: : (colon); compound statement 151 152The :keyword:`for` statement is used to iterate over the elements of a sequence 153(such as a string, tuple or list) or other iterable object: 154 155.. productionlist:: python-grammar 156 for_stmt: "for" `target_list` "in" `expression_list` ":" `suite` 157 : ["else" ":" `suite`] 158 159The expression list is evaluated once; it should yield an iterable object. An 160iterator is created for the result of the ``expression_list``. The suite is 161then executed once for each item provided by the iterator, in the order returned 162by the iterator. Each item in turn is assigned to the target list using the 163standard rules for assignments (see :ref:`assignment`), and then the suite is 164executed. When the items are exhausted (which is immediately when the sequence 165is empty or an iterator raises a :exc:`StopIteration` exception), the suite in 166the :keyword:`!else` clause, if present, is executed, and the loop terminates. 167 168.. index:: 169 statement: break 170 statement: continue 171 172A :keyword:`break` statement executed in the first suite terminates the loop 173without executing the :keyword:`!else` clause's suite. A :keyword:`continue` 174statement executed in the first suite skips the rest of the suite and continues 175with the next item, or with the :keyword:`!else` clause if there is no next 176item. 177 178The for-loop makes assignments to the variables in the target list. 179This overwrites all previous assignments to those variables including 180those made in the suite of the for-loop:: 181 182 for i in range(10): 183 print(i) 184 i = 5 # this will not affect the for-loop 185 # because i will be overwritten with the next 186 # index in the range 187 188 189.. index:: 190 builtin: range 191 192Names in the target list are not deleted when the loop is finished, but if the 193sequence is empty, they will not have been assigned to at all by the loop. Hint: 194the built-in function :func:`range` returns an iterator of integers suitable to 195emulate the effect of Pascal's ``for i := a to b do``; e.g., ``list(range(3))`` 196returns the list ``[0, 1, 2]``. 197 198.. note:: 199 200 .. index:: 201 single: loop; over mutable sequence 202 single: mutable sequence; loop over 203 204 There is a subtlety when the sequence is being modified by the loop (this can 205 only occur for mutable sequences, e.g. lists). An internal counter is used 206 to keep track of which item is used next, and this is incremented on each 207 iteration. When this counter has reached the length of the sequence the loop 208 terminates. This means that if the suite deletes the current (or a previous) 209 item from the sequence, the next item will be skipped (since it gets the 210 index of the current item which has already been treated). Likewise, if the 211 suite inserts an item in the sequence before the current item, the current 212 item will be treated again the next time through the loop. This can lead to 213 nasty bugs that can be avoided by making a temporary copy using a slice of 214 the whole sequence, e.g., :: 215 216 for x in a[:]: 217 if x < 0: a.remove(x) 218 219 220.. _try: 221.. _except: 222.. _finally: 223 224The :keyword:`!try` statement 225============================= 226 227.. index:: 228 ! statement: try 229 keyword: except 230 keyword: finally 231 keyword: else 232 keyword: as 233 single: : (colon); compound statement 234 235The :keyword:`try` statement specifies exception handlers and/or cleanup code 236for a group of statements: 237 238.. productionlist:: python-grammar 239 try_stmt: `try1_stmt` | `try2_stmt` 240 try1_stmt: "try" ":" `suite` 241 : ("except" [`expression` ["as" `identifier`]] ":" `suite`)+ 242 : ["else" ":" `suite`] 243 : ["finally" ":" `suite`] 244 try2_stmt: "try" ":" `suite` 245 : "finally" ":" `suite` 246 247 248The :keyword:`except` clause(s) specify one or more exception handlers. When no 249exception occurs in the :keyword:`try` clause, no exception handler is executed. 250When an exception occurs in the :keyword:`!try` suite, a search for an exception 251handler is started. This search inspects the except clauses in turn until one 252is found that matches the exception. An expression-less except clause, if 253present, must be last; it matches any exception. For an except clause with an 254expression, that expression is evaluated, and the clause matches the exception 255if the resulting object is "compatible" with the exception. An object is 256compatible with an exception if it is the class or a base class of the exception 257object, or a tuple containing an item that is the class or a base class of 258the exception object. 259 260If no except clause matches the exception, the search for an exception handler 261continues in the surrounding code and on the invocation stack. [#]_ 262 263If the evaluation of an expression in the header of an except clause raises an 264exception, the original search for a handler is canceled and a search starts for 265the new exception in the surrounding code and on the call stack (it is treated 266as if the entire :keyword:`try` statement raised the exception). 267 268.. index:: single: as; except clause 269 270When a matching except clause is found, the exception is assigned to the target 271specified after the :keyword:`!as` keyword in that except clause, if present, and 272the except clause's suite is executed. All except clauses must have an 273executable block. When the end of this block is reached, execution continues 274normally after the entire try statement. (This means that if two nested 275handlers exist for the same exception, and the exception occurs in the try 276clause of the inner handler, the outer handler will not handle the exception.) 277 278When an exception has been assigned using ``as target``, it is cleared at the 279end of the except clause. This is as if :: 280 281 except E as N: 282 foo 283 284was translated to :: 285 286 except E as N: 287 try: 288 foo 289 finally: 290 del N 291 292This means the exception must be assigned to a different name to be able to 293refer to it after the except clause. Exceptions are cleared because with the 294traceback attached to them, they form a reference cycle with the stack frame, 295keeping all locals in that frame alive until the next garbage collection occurs. 296 297.. index:: 298 module: sys 299 object: traceback 300 301Before an except clause's suite is executed, details about the exception are 302stored in the :mod:`sys` module and can be accessed via :func:`sys.exc_info`. 303:func:`sys.exc_info` returns a 3-tuple consisting of the exception class, the 304exception instance and a traceback object (see section :ref:`types`) identifying 305the point in the program where the exception occurred. :func:`sys.exc_info` 306values are restored to their previous values (before the call) when returning 307from a function that handled an exception. 308 309.. index:: 310 keyword: else 311 statement: return 312 statement: break 313 statement: continue 314 315The optional :keyword:`!else` clause is executed if the control flow leaves the 316:keyword:`try` suite, no exception was raised, and no :keyword:`return`, 317:keyword:`continue`, or :keyword:`break` statement was executed. Exceptions in 318the :keyword:`!else` clause are not handled by the preceding :keyword:`except` 319clauses. 320 321.. index:: keyword: finally 322 323If :keyword:`finally` is present, it specifies a 'cleanup' handler. The 324:keyword:`try` clause is executed, including any :keyword:`except` and 325:keyword:`!else` clauses. If an exception occurs in any of the clauses and is 326not handled, the exception is temporarily saved. The :keyword:`!finally` clause 327is executed. If there is a saved exception it is re-raised at the end of the 328:keyword:`!finally` clause. If the :keyword:`!finally` clause raises another 329exception, the saved exception is set as the context of the new exception. 330If the :keyword:`!finally` clause executes a :keyword:`return`, :keyword:`break` 331or :keyword:`continue` statement, the saved exception is discarded:: 332 333 >>> def f(): 334 ... try: 335 ... 1/0 336 ... finally: 337 ... return 42 338 ... 339 >>> f() 340 42 341 342The exception information is not available to the program during execution of 343the :keyword:`finally` clause. 344 345.. index:: 346 statement: return 347 statement: break 348 statement: continue 349 350When a :keyword:`return`, :keyword:`break` or :keyword:`continue` statement is 351executed in the :keyword:`try` suite of a :keyword:`!try`...\ :keyword:`!finally` 352statement, the :keyword:`finally` clause is also executed 'on the way out.' 353 354The return value of a function is determined by the last :keyword:`return` 355statement executed. Since the :keyword:`finally` clause always executes, a 356:keyword:`!return` statement executed in the :keyword:`!finally` clause will 357always be the last one executed:: 358 359 >>> def foo(): 360 ... try: 361 ... return 'try' 362 ... finally: 363 ... return 'finally' 364 ... 365 >>> foo() 366 'finally' 367 368Additional information on exceptions can be found in section :ref:`exceptions`, 369and information on using the :keyword:`raise` statement to generate exceptions 370may be found in section :ref:`raise`. 371 372.. versionchanged:: 3.8 373 Prior to Python 3.8, a :keyword:`continue` statement was illegal in the 374 :keyword:`finally` clause due to a problem with the implementation. 375 376 377.. _with: 378.. _as: 379 380The :keyword:`!with` statement 381============================== 382 383.. index:: 384 ! statement: with 385 keyword: as 386 single: as; with statement 387 single: , (comma); with statement 388 single: : (colon); compound statement 389 390The :keyword:`with` statement is used to wrap the execution of a block with 391methods defined by a context manager (see section :ref:`context-managers`). 392This allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally` 393usage patterns to be encapsulated for convenient reuse. 394 395.. productionlist:: python-grammar 396 with_stmt: "with" `with_item` ("," `with_item`)* ":" `suite` 397 with_item: `expression` ["as" `target`] 398 399The execution of the :keyword:`with` statement with one "item" proceeds as follows: 400 401#. The context expression (the expression given in the :token:`with_item`) is 402 evaluated to obtain a context manager. 403 404#. The context manager's :meth:`__enter__` is loaded for later use. 405 406#. The context manager's :meth:`__exit__` is loaded for later use. 407 408#. The context manager's :meth:`__enter__` method is invoked. 409 410#. If a target was included in the :keyword:`with` statement, the return value 411 from :meth:`__enter__` is assigned to it. 412 413 .. note:: 414 415 The :keyword:`with` statement guarantees that if the :meth:`__enter__` 416 method returns without an error, then :meth:`__exit__` will always be 417 called. Thus, if an error occurs during the assignment to the target list, 418 it will be treated the same as an error occurring within the suite would 419 be. See step 6 below. 420 421#. The suite is executed. 422 423#. The context manager's :meth:`__exit__` method is invoked. If an exception 424 caused the suite to be exited, its type, value, and traceback are passed as 425 arguments to :meth:`__exit__`. Otherwise, three :const:`None` arguments are 426 supplied. 427 428 If the suite was exited due to an exception, and the return value from the 429 :meth:`__exit__` method was false, the exception is reraised. If the return 430 value was true, the exception is suppressed, and execution continues with the 431 statement following the :keyword:`with` statement. 432 433 If the suite was exited for any reason other than an exception, the return 434 value from :meth:`__exit__` is ignored, and execution proceeds at the normal 435 location for the kind of exit that was taken. 436 437The following code:: 438 439 with EXPRESSION as TARGET: 440 SUITE 441 442is semantically equivalent to:: 443 444 manager = (EXPRESSION) 445 enter = type(manager).__enter__ 446 exit = type(manager).__exit__ 447 value = enter(manager) 448 hit_except = False 449 450 try: 451 TARGET = value 452 SUITE 453 except: 454 hit_except = True 455 if not exit(manager, *sys.exc_info()): 456 raise 457 finally: 458 if not hit_except: 459 exit(manager, None, None, None) 460 461With more than one item, the context managers are processed as if multiple 462:keyword:`with` statements were nested:: 463 464 with A() as a, B() as b: 465 SUITE 466 467is semantically equivalent to:: 468 469 with A() as a: 470 with B() as b: 471 SUITE 472 473.. versionchanged:: 3.1 474 Support for multiple context expressions. 475 476.. seealso:: 477 478 :pep:`343` - The "with" statement 479 The specification, background, and examples for the Python :keyword:`with` 480 statement. 481 482 483.. index:: 484 single: parameter; function definition 485 486.. _function: 487.. _def: 488 489Function definitions 490==================== 491 492.. index:: 493 statement: def 494 pair: function; definition 495 pair: function; name 496 pair: name; binding 497 object: user-defined function 498 object: function 499 pair: function; name 500 pair: name; binding 501 single: () (parentheses); function definition 502 single: , (comma); parameter list 503 single: : (colon); compound statement 504 505A function definition defines a user-defined function object (see section 506:ref:`types`): 507 508.. productionlist:: python-grammar 509 funcdef: [`decorators`] "def" `funcname` "(" [`parameter_list`] ")" 510 : ["->" `expression`] ":" `suite` 511 decorators: `decorator`+ 512 decorator: "@" `dotted_name` ["(" [`argument_list` [","]] ")"] NEWLINE 513 dotted_name: `identifier` ("." `identifier`)* 514 parameter_list: `defparameter` ("," `defparameter`)* "," "/" ["," [`parameter_list_no_posonly`]] 515 : | `parameter_list_no_posonly` 516 parameter_list_no_posonly: `defparameter` ("," `defparameter`)* ["," [`parameter_list_starargs`]] 517 : | `parameter_list_starargs` 518 parameter_list_starargs: "*" [`parameter`] ("," `defparameter`)* ["," ["**" `parameter` [","]]] 519 : | "**" `parameter` [","] 520 parameter: `identifier` [":" `expression`] 521 defparameter: `parameter` ["=" `expression`] 522 funcname: `identifier` 523 524 525A function definition is an executable statement. Its execution binds the 526function name in the current local namespace to a function object (a wrapper 527around the executable code for the function). This function object contains a 528reference to the current global namespace as the global namespace to be used 529when the function is called. 530 531The function definition does not execute the function body; this gets executed 532only when the function is called. [#]_ 533 534.. index:: 535 single: @ (at); function definition 536 537A function definition may be wrapped by one or more :term:`decorator` expressions. 538Decorator expressions are evaluated when the function is defined, in the scope 539that contains the function definition. The result must be a callable, which is 540invoked with the function object as the only argument. The returned value is 541bound to the function name instead of the function object. Multiple decorators 542are applied in nested fashion. For example, the following code :: 543 544 @f1(arg) 545 @f2 546 def func(): pass 547 548is roughly equivalent to :: 549 550 def func(): pass 551 func = f1(arg)(f2(func)) 552 553except that the original function is not temporarily bound to the name ``func``. 554 555.. index:: 556 triple: default; parameter; value 557 single: argument; function definition 558 single: = (equals); function definition 559 560When one or more :term:`parameters <parameter>` have the form *parameter* ``=`` 561*expression*, the function is said to have "default parameter values." For a 562parameter with a default value, the corresponding :term:`argument` may be 563omitted from a call, in which 564case the parameter's default value is substituted. If a parameter has a default 565value, all following parameters up until the "``*``" must also have a default 566value --- this is a syntactic restriction that is not expressed by the grammar. 567 568**Default parameter values are evaluated from left to right when the function 569definition is executed.** This means that the expression is evaluated once, when 570the function is defined, and that the same "pre-computed" value is used for each 571call. This is especially important to understand when a default parameter is a 572mutable object, such as a list or a dictionary: if the function modifies the 573object (e.g. by appending an item to a list), the default value is in effect 574modified. This is generally not what was intended. A way around this is to use 575``None`` as the default, and explicitly test for it in the body of the function, 576e.g.:: 577 578 def whats_on_the_telly(penguin=None): 579 if penguin is None: 580 penguin = [] 581 penguin.append("property of the zoo") 582 return penguin 583 584.. index:: 585 single: / (slash); function definition 586 single: * (asterisk); function definition 587 single: **; function definition 588 589Function call semantics are described in more detail in section :ref:`calls`. A 590function call always assigns values to all parameters mentioned in the parameter 591list, either from positional arguments, from keyword arguments, or from default 592values. If the form "``*identifier``" is present, it is initialized to a tuple 593receiving any excess positional parameters, defaulting to the empty tuple. 594If the form "``**identifier``" is present, it is initialized to a new 595ordered mapping receiving any excess keyword arguments, defaulting to a 596new empty mapping of the same type. Parameters after "``*``" or 597"``*identifier``" are keyword-only parameters and may only be passed 598by keyword arguments. Parameters before "``/``" are positional-only parameters 599and may only be passed by positional arguments. 600 601.. versionchanged:: 3.8 602 The ``/`` function parameter syntax may be used to indicate positional-only 603 parameters. See :pep:`570` for details. 604 605.. index:: 606 pair: function; annotations 607 single: ->; function annotations 608 single: : (colon); function annotations 609 610Parameters may have an :term:`annotation <function annotation>` of the form "``: expression``" 611following the parameter name. Any parameter may have an annotation, even those of the form 612``*identifier`` or ``**identifier``. Functions may have "return" annotation of 613the form "``-> expression``" after the parameter list. These annotations can be 614any valid Python expression. The presence of annotations does not change the 615semantics of a function. The annotation values are available as values of 616a dictionary keyed by the parameters' names in the :attr:`__annotations__` 617attribute of the function object. If the ``annotations`` import from 618:mod:`__future__` is used, annotations are preserved as strings at runtime which 619enables postponed evaluation. Otherwise, they are evaluated when the function 620definition is executed. In this case annotations may be evaluated in 621a different order than they appear in the source code. 622 623.. index:: pair: lambda; expression 624 625It is also possible to create anonymous functions (functions not bound to a 626name), for immediate use in expressions. This uses lambda expressions, described in 627section :ref:`lambda`. Note that the lambda expression is merely a shorthand for a 628simplified function definition; a function defined in a ":keyword:`def`" 629statement can be passed around or assigned to another name just like a function 630defined by a lambda expression. The ":keyword:`!def`" form is actually more powerful 631since it allows the execution of multiple statements and annotations. 632 633**Programmer's note:** Functions are first-class objects. A "``def``" statement 634executed inside a function definition defines a local function that can be 635returned or passed around. Free variables used in the nested function can 636access the local variables of the function containing the def. See section 637:ref:`naming` for details. 638 639.. seealso:: 640 641 :pep:`3107` - Function Annotations 642 The original specification for function annotations. 643 644 :pep:`484` - Type Hints 645 Definition of a standard meaning for annotations: type hints. 646 647 :pep:`526` - Syntax for Variable Annotations 648 Ability to type hint variable declarations, including class 649 variables and instance variables 650 651 :pep:`563` - Postponed Evaluation of Annotations 652 Support for forward references within annotations by preserving 653 annotations in a string form at runtime instead of eager evaluation. 654 655 656.. _class: 657 658Class definitions 659================= 660 661.. index:: 662 object: class 663 statement: class 664 pair: class; definition 665 pair: class; name 666 pair: name; binding 667 pair: execution; frame 668 single: inheritance 669 single: docstring 670 single: () (parentheses); class definition 671 single: , (comma); expression list 672 single: : (colon); compound statement 673 674A class definition defines a class object (see section :ref:`types`): 675 676.. productionlist:: python-grammar 677 classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite` 678 inheritance: "(" [`argument_list`] ")" 679 classname: `identifier` 680 681A class definition is an executable statement. The inheritance list usually 682gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so 683each item in the list should evaluate to a class object which allows 684subclassing. Classes without an inheritance list inherit, by default, from the 685base class :class:`object`; hence, :: 686 687 class Foo: 688 pass 689 690is equivalent to :: 691 692 class Foo(object): 693 pass 694 695The class's suite is then executed in a new execution frame (see :ref:`naming`), 696using a newly created local namespace and the original global namespace. 697(Usually, the suite contains mostly function definitions.) When the class's 698suite finishes execution, its execution frame is discarded but its local 699namespace is saved. [#]_ A class object is then created using the inheritance 700list for the base classes and the saved local namespace for the attribute 701dictionary. The class name is bound to this class object in the original local 702namespace. 703 704The order in which attributes are defined in the class body is preserved 705in the new class's ``__dict__``. Note that this is reliable only right 706after the class is created and only for classes that were defined using 707the definition syntax. 708 709Class creation can be customized heavily using :ref:`metaclasses <metaclasses>`. 710 711.. index:: 712 single: @ (at); class definition 713 714Classes can also be decorated: just like when decorating functions, :: 715 716 @f1(arg) 717 @f2 718 class Foo: pass 719 720is roughly equivalent to :: 721 722 class Foo: pass 723 Foo = f1(arg)(f2(Foo)) 724 725The evaluation rules for the decorator expressions are the same as for function 726decorators. The result is then bound to the class name. 727 728**Programmer's note:** Variables defined in the class definition are class 729attributes; they are shared by instances. Instance attributes can be set in a 730method with ``self.name = value``. Both class and instance attributes are 731accessible through the notation "``self.name``", and an instance attribute hides 732a class attribute with the same name when accessed in this way. Class 733attributes can be used as defaults for instance attributes, but using mutable 734values there can lead to unexpected results. :ref:`Descriptors <descriptors>` 735can be used to create instance variables with different implementation details. 736 737 738.. seealso:: 739 740 :pep:`3115` - Metaclasses in Python 3000 741 The proposal that changed the declaration of metaclasses to the current 742 syntax, and the semantics for how classes with metaclasses are 743 constructed. 744 745 :pep:`3129` - Class Decorators 746 The proposal that added class decorators. Function and method decorators 747 were introduced in :pep:`318`. 748 749 750.. _async: 751 752Coroutines 753========== 754 755.. versionadded:: 3.5 756 757.. index:: statement: async def 758.. _`async def`: 759 760Coroutine function definition 761----------------------------- 762 763.. productionlist:: python-grammar 764 async_funcdef: [`decorators`] "async" "def" `funcname` "(" [`parameter_list`] ")" 765 : ["->" `expression`] ":" `suite` 766 767.. index:: 768 keyword: async 769 keyword: await 770 771Execution of Python coroutines can be suspended and resumed at many points 772(see :term:`coroutine`). Inside the body of a coroutine function, ``await`` and 773``async`` identifiers become reserved keywords; :keyword:`await` expressions, 774:keyword:`async for` and :keyword:`async with` can only be used in 775coroutine function bodies. 776 777Functions defined with ``async def`` syntax are always coroutine functions, 778even if they do not contain ``await`` or ``async`` keywords. 779 780It is a :exc:`SyntaxError` to use a ``yield from`` expression inside the body 781of a coroutine function. 782 783An example of a coroutine function:: 784 785 async def func(param1, param2): 786 do_stuff() 787 await some_coroutine() 788 789 790.. index:: statement: async for 791.. _`async for`: 792 793The :keyword:`!async for` statement 794----------------------------------- 795 796.. productionlist:: python-grammar 797 async_for_stmt: "async" `for_stmt` 798 799An :term:`asynchronous iterable` is able to call asynchronous code in its 800*iter* implementation, and :term:`asynchronous iterator` can call asynchronous 801code in its *next* method. 802 803The ``async for`` statement allows convenient iteration over asynchronous 804iterators. 805 806The following code:: 807 808 async for TARGET in ITER: 809 SUITE 810 else: 811 SUITE2 812 813Is semantically equivalent to:: 814 815 iter = (ITER) 816 iter = type(iter).__aiter__(iter) 817 running = True 818 819 while running: 820 try: 821 TARGET = await type(iter).__anext__(iter) 822 except StopAsyncIteration: 823 running = False 824 else: 825 SUITE 826 else: 827 SUITE2 828 829See also :meth:`__aiter__` and :meth:`__anext__` for details. 830 831It is a :exc:`SyntaxError` to use an ``async for`` statement outside the 832body of a coroutine function. 833 834 835.. index:: statement: async with 836.. _`async with`: 837 838The :keyword:`!async with` statement 839------------------------------------ 840 841.. productionlist:: python-grammar 842 async_with_stmt: "async" `with_stmt` 843 844An :term:`asynchronous context manager` is a :term:`context manager` that is 845able to suspend execution in its *enter* and *exit* methods. 846 847The following code:: 848 849 async with EXPRESSION as TARGET: 850 SUITE 851 852is semantically equivalent to:: 853 854 manager = (EXPRESSION) 855 aexit = type(manager).__aexit__ 856 aenter = type(manager).__aenter__ 857 value = await aenter(manager) 858 hit_except = False 859 860 try: 861 TARGET = value 862 SUITE 863 except: 864 hit_except = True 865 if not await aexit(manager, *sys.exc_info()): 866 raise 867 finally: 868 if not hit_except: 869 await aexit(manager, None, None, None) 870 871See also :meth:`__aenter__` and :meth:`__aexit__` for details. 872 873It is a :exc:`SyntaxError` to use an ``async with`` statement outside the 874body of a coroutine function. 875 876.. seealso:: 877 878 :pep:`492` - Coroutines with async and await syntax 879 The proposal that made coroutines a proper standalone concept in Python, 880 and added supporting syntax. 881 882 883.. rubric:: Footnotes 884 885.. [#] The exception is propagated to the invocation stack unless 886 there is a :keyword:`finally` clause which happens to raise another 887 exception. That new exception causes the old one to be lost. 888 889.. [#] A string literal appearing as the first statement in the function body is 890 transformed into the function's ``__doc__`` attribute and therefore the 891 function's :term:`docstring`. 892 893.. [#] A string literal appearing as the first statement in the class body is 894 transformed into the namespace's ``__doc__`` item and therefore the class's 895 :term:`docstring`. 896