===================
.. index::
- statement: try
- statement: except
+ pair: statement; try
+ pair: statement; except
In Python, all exceptions must be instances of a class that derives from
:class:`BaseException`. In a :keyword:`try` statement with an :keyword:`except`
derived). Two exception classes that are not related via subclassing are never
equivalent, even if they have the same name.
-.. index:: statement: raise
+.. index:: pair: statement; raise
The built-in exceptions listed below can be generated by the interpreter or
built-in functions. Except where mentioned, they have an "associated value"
.. exception:: AssertionError
- .. index:: statement: assert
+ .. index:: pair: statement; assert
Raised when an :keyword:`assert` statement fails.
.. function:: __import__(name, globals=None, locals=None, fromlist=(), level=0)
.. index::
- statement: import
+ pair: statement; import
pair: module; builtins
.. note::
.. index::
single: # (hash); comment
- statement: import
+ pair: statement; import
A path configuration file is a file whose name has the form :file:`{name}.pth`
and exists in one of the four directories mentioned above; its contents are
===================
.. index::
- statement: if
- statement: while
+ pair: statement; if
+ pair: statement; while
pair: truth; value
pair: Boolean; operations
single: false
triple: operations on; list; type
pair: subscript; assignment
pair: slice; assignment
- statement: del
+ pair: statement; del
single: append() (sequence method)
single: clear() (sequence method)
single: copy() (sequence method)
pair: object; dictionary
triple: operations on; mapping; types
triple: operations on; dictionary; type
- statement: del
+ pair: statement; del
builtin: len
A :term:`mapping` object maps :term:`hashable` values to arbitrary objects.
============================
.. index::
- ! statement: if
+ ! pair: statement; if
pair: keyword; elif
pair: keyword; else
single: : (colon); compound statement
===============================
.. index::
- ! statement: while
+ ! pair: statement; while
pair: keyword; else
pair: loop; statement
single: : (colon); compound statement
terminates.
.. index::
- statement: break
- statement: continue
+ pair: statement; break
+ pair: statement; continue
A :keyword:`break` statement executed in the first suite terminates the loop
without executing the :keyword:`!else` clause's suite. A :keyword:`continue`
=============================
.. index::
- ! statement: for
+ ! pair: statement; for
pair: keyword; in
pair: keyword; else
pair: target; list
if present, is executed, and the loop terminates.
.. index::
- statement: break
- statement: continue
+ pair: statement; break
+ pair: statement; continue
A :keyword:`break` statement executed in the first suite terminates the loop
without executing the :keyword:`!else` clause's suite. A :keyword:`continue`
=============================
.. index::
- ! statement: try
+ ! pair: statement; try
pair: keyword; except
pair: keyword; finally
pair: keyword; else
.. index::
pair: keyword; else
- statement: return
- statement: break
- statement: continue
+ pair: statement; return
+ pair: statement; break
+ pair: statement; continue
.. _except_else:
the :keyword:`!finally` clause.
.. index::
- statement: return
- statement: break
- statement: continue
+ pair: statement; return
+ pair: statement; break
+ pair: statement; continue
When a :keyword:`return`, :keyword:`break` or :keyword:`continue` statement is
executed in the :keyword:`try` suite of a :keyword:`!try`...\ :keyword:`!finally`
==============================
.. index::
- ! statement: with
+ ! pair: statement; with
pair: keyword; as
single: as; with statement
single: , (comma); with statement
===============================
.. index::
- ! statement: match
+ ! pair: statement; match
! pair: keyword; case
! single: pattern matching
pair: keyword; if
====================
.. index::
- statement: def
+ pair: statement; def
pair: function; definition
pair: function; name
pair: name; binding
.. index::
pair: object; class
- statement: class
+ pair: statement; class
pair: class; definition
pair: class; name
pair: name; binding
.. versionadded:: 3.5
-.. index:: statement: async def
+.. index:: pair: statement; async def
.. _`async def`:
Coroutine function definition
``await`` and ``async`` are now keywords; previously they were only
treated as such inside the body of a coroutine function.
-.. index:: statement: async for
+.. index:: pair: statement; async for
.. _`async for`:
The :keyword:`!async for` statement
body of a coroutine function.
-.. index:: statement: async with
+.. index:: pair: statement; async with
.. _`async with`:
The :keyword:`!async with` statement
Modules
.. index::
- statement: import
+ pair: statement; import
pair: object; module
Modules are a basic organizational unit of Python code, and are created by
single: tb_frame (traceback attribute)
single: tb_lineno (traceback attribute)
single: tb_lasti (traceback attribute)
- statement: try
+ pair: statement; try
Special read-only attributes:
:attr:`tb_frame` points to the execution frame of the current level;
.. index::
single: destructor
single: finalizer
- statement: del
+ pair: statement; del
Called when the instance is about to be destroyed. This is also called a
finalizer or (improperly) a destructor. If a base class has a
used by directly invoking their methods.
.. index::
- statement: with
+ pair: statement; with
single: context manager
Typical uses of context managers include saving and restoring various kinds of
================================
.. index::
- ! statement: assert
+ ! pair: statement; assert
pair: debugging; assertions
single: , (comma); expression list
==============================
.. index::
- statement: pass
+ pair: statement; pass
pair: null; operation
pair: null; operation
=============================
.. index::
- ! statement: del
+ ! pair: statement; del
pair: deletion; target
triple: deletion; target; list
Deletion of a target list recursively deletes each target, from left to right.
.. index::
- statement: global
+ pair: statement; global
pair: unbinding; name
Deletion of a name removes the binding of that name from the local or global
================================
.. index::
- ! statement: return
+ ! pair: statement; return
pair: function; definition
pair: class; definition
===============================
.. index::
- statement: yield
+ pair: statement; yield
single: generator; function
single: generator; iterator
single: function; generator
===============================
.. index::
- ! statement: raise
+ ! pair: statement; raise
single: exception
pair: raising; exception
single: __traceback__ (exception attribute)
===============================
.. index::
- ! statement: break
- statement: for
- statement: while
+ ! pair: statement; break
+ pair: statement; for
+ pair: statement; while
pair: loop; statement
.. productionlist:: python-grammar
==================================
.. index::
- ! statement: continue
- statement: for
- statement: while
+ ! pair: statement; continue
+ pair: statement; for
+ pair: statement; while
pair: loop; statement
pair: keyword; finally
================================
.. index::
- ! statement: import
+ ! pair: statement; import
single: module; importing
pair: name; binding
pair: keyword; from
================================
.. index::
- ! statement: global
+ ! pair: statement; global
triple: global; name; binding
single: , (comma); identifier list
The :keyword:`!nonlocal` statement
==================================
-.. index:: statement: nonlocal
+.. index:: pair: statement; nonlocal
single: , (comma); identifier list
.. productionlist:: python-grammar
pairindextypes.pop('operator', None)
pairindextypes.pop('object', None)
pairindextypes.pop('exception', None)
- # pairindextypes.pop('statement', None)
+ pairindextypes.pop('statement', None)
# pairindextypes.pop('builtin', None)
==========================
.. index::
- statement: for
+ pair: statement; for
The :keyword:`for` statement in Python differs a bit from what you may be used
to in C or Pascal. Rather than always iterating over an arithmetic progression