Changeset 391 for python/trunk/Doc/reference/compound_stmts.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/reference/compound_stmts.rst
r2 r391 239 239 if the resulting object is "compatible" with the exception. An object is 240 240 compatible with an exception if it is the class or a base class of the exception 241 object, a tuple containing an item compatible with the exception, or, in the 242 (deprecated) case of string exceptions, is the raised string itself (note that 243 the object identities must match, i.e. it must be the same string object, not 244 just a string with the same value). 241 object, or a tuple containing an item compatible with the exception. 245 242 246 243 If no except clause matches the exception, the search for an exception handler … … 298 295 :keyword:`finally` clause. If the :keyword:`finally` clause raises another 299 296 exception or executes a :keyword:`return` or :keyword:`break` statement, the 300 saved exception is lost. The exception information is not available to the 301 program during execution of the :keyword:`finally` clause. 297 saved exception is discarded:: 298 299 def f(): 300 try: 301 1/0 302 finally: 303 return 42 304 305 >>> f() 306 42 307 308 The exception information is not available to the program during execution of 309 the :keyword:`finally` clause. 302 310 303 311 .. index:: … … 334 342 335 343 .. productionlist:: 336 with_stmt: "with" `expression` ["as" `target`] ":" `suite` 337 338 The execution of the :keyword:`with` statement proceeds as follows: 339 340 #. The context expression is evaluated to obtain a context manager. 344 with_stmt: "with" with_item ("," with_item)* ":" `suite` 345 with_item: `expression` ["as" `target`] 346 347 The execution of the :keyword:`with` statement with one "item" proceeds as follows: 348 349 #. The context expression (the expression given in the :token:`with_item`) is 350 evaluated to obtain a context manager. 351 352 #. The context manager's :meth:`__exit__` is loaded for later use. 341 353 342 354 #. The context manager's :meth:`__enter__` method is invoked. … … 350 362 returns without an error, then :meth:`__exit__` will always be called. Thus, if 351 363 an error occurs during the assignment to the target list, it will be treated the 352 same as an error occurring within the suite would be. See step 5below.364 same as an error occurring within the suite would be. See step 6 below. 353 365 354 366 #. The suite is executed. … … 368 380 for the kind of exit that was taken. 369 381 382 With more than one item, the context managers are processed as if multiple 383 :keyword:`with` statements were nested:: 384 385 with A() as a, B() as b: 386 suite 387 388 is equivalent to :: 389 390 with A() as a: 391 with B() as b: 392 suite 393 370 394 .. note:: 371 395 … … 374 398 Python 2.6. 375 399 400 .. versionchanged:: 2.7 401 Support for multiple context expressions. 402 376 403 .. seealso:: 377 404 … … 380 407 statement. 381 408 409 410 .. index:: 411 single: parameter; function definition 382 412 383 413 .. _function: … … 405 435 dotted_name: `identifier` ("." `identifier`)* 406 436 parameter_list: (`defparameter` ",")* 407 : ( "*" `identifier` [ ,"**" `identifier`]437 : ( "*" `identifier` ["," "**" `identifier`] 408 438 : | "**" `identifier` 409 439 : | `defparameter` [","] ) … … 441 471 func = f1(arg)(f2(func)) 442 472 443 .. index:: triple: default; parameter; value 444 445 When one or more top-level parameters have the form *parameter* ``=`` 446 *expression*, the function is said to have "default parameter values." For a 447 parameter with a default value, the corresponding argument may be omitted from a 448 call, in which case the parameter's default value is substituted. If a 473 .. index:: 474 triple: default; parameter; value 475 single: argument; function definition 476 477 When one or more top-level :term:`parameters <parameter>` have the form 478 *parameter* ``=`` *expression*, the function is said to have "default parameter 479 values." For a parameter with a default value, the corresponding 480 :term:`argument` may be omitted from a call, in which 481 case the parameter's default value is substituted. If a 449 482 parameter has a default value, all following parameters must also have a default 450 483 value --- this is a syntactic restriction that is not expressed by the grammar. … … 452 485 **Default parameter values are evaluated when the function definition is 453 486 executed.** This means that the expression is evaluated once, when the function 454 is defined, and that th atsame "pre-computed" value is used for each call. This487 is defined, and that the same "pre-computed" value is used for each call. This 455 488 is especially important to understand when a default parameter is a mutable 456 489 object, such as a list or a dictionary: if the function modifies the object … … 477 510 receiving any excess keyword arguments, defaulting to a new empty dictionary. 478 511 479 .. index:: pair: lambda; form512 .. index:: pair: lambda; expression 480 513 481 514 It is also possible to create anonymous functions (functions not bound to a 482 name), for immediate use in expressions. This uses lambda forms, described in483 section :ref:`lambda`. Note that the lambda formis merely a shorthand for a515 name), for immediate use in expressions. This uses lambda expressions, described in 516 section :ref:`lambda`. Note that the lambda expression is merely a shorthand for a 484 517 simplified function definition; a function defined in a ":keyword:`def`" 485 518 statement can be passed around or assigned to another name just like a function 486 defined by a lambda form. The ":keyword:`def`" form is actually more powerful519 defined by a lambda expression. The ":keyword:`def`" form is actually more powerful 487 520 since it allows the execution of multiple statements. 488 521 … … 544 577 .. rubric:: Footnotes 545 578 546 .. [#] The exception is propagated to the invocation stack only if there is no 547 :keyword:`finally` clause that negates the exception. 579 .. [#] The exception is propagated to the invocation stack unless 580 there is a :keyword:`finally` clause which happens to raise another 581 exception. That new exception causes the old one to be lost. 548 582 549 583 .. [#] Currently, control "flows off the end" except in the case of an exception or the
Note:
See TracChangeset
for help on using the changeset viewer.