Skip to content

Commit 14afaf0

Browse files
author
georg.brandl
committed
#4247: add "pass" examples to tutorial.
git-svn-id: http://svn.python.org/projects/python/trunk@67123 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4dd1e91 commit 14afaf0

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Doc/tutorial/controlflow.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,39 @@ required syntactically but the program requires no action. For example::
166166
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
167167
...
168168

169+
This is commonly used for creating minimal classes like with exceptions, or
170+
for skipping unwanted exceptions::
171+
172+
>>> class ParserError(Exception):
173+
... pass
174+
...
175+
>>> try:
176+
... import audioop
177+
... except ImportError:
178+
... pass
179+
...
180+
181+
Another place it can be used is as a place-holder for a function or
182+
conditional body when you are working on new code, allowing you to keep
183+
thinking at a more abstract level. However, as :keyword:`pass` is silently
184+
ignored, a better choice may be to raise a :exc:`NotImplementedError`
185+
exception::
186+
187+
>>> def initlog(*args):
188+
... raise NotImplementedError # Open logfile if not already open
189+
... if not logfp:
190+
... raise NotImplementedError # Set up dummy log back-end
191+
... raise NotImplementedError('Call log initialization handler')
192+
...
193+
194+
If :keyword:`pass` were used here and you later ran tests, they may fail
195+
without indicating why. Using :exc:`NotImplementedError` causes this code
196+
to raise an exception, allowing you to tell exactly where code that you
197+
need to complete is. Note the two call styles of the exceptions above.
198+
The comment style is useful in that when you remove the exception you can
199+
easily leave the comment, which ideally would be a good description for
200+
the block of code the exception is a placeholder for. The call-style
201+
will raise a more useful exception however.
169202

170203
.. _tut-functions:
171204

0 commit comments

Comments
 (0)