This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Docs: Provide some examples of "pass" use in the tutorial.
Type: enhancement Stage:
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: fdrake Nosy List: LambertDW, fdrake, georg.brandl, jafo
Priority: normal Keywords: easy, needs review, patch

Created on 2008-11-02 02:16 by jafo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pass-examples.patch jafo, 2008-11-02 02:15
pass-examples-2.patch jafo, 2008-11-06 00:11 New version of the patch.
Messages (5)
msg75452 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2008-11-02 02:15
I'm giving a Python tutorial to a bunch of local people and have decided
to use the Python.org tutorial to go by.  One of the things I found I
wanted in the section on the "pass" statement was more information about
it's use.  But I also think it's worth mentioning using the
NotImplementedError if pass is used for stubbing out code.

This patch to the tutorial includes some example use cases, and also a
reference to NotImplementedError.  I would appreciate some review
however, as I'm not sure this discussion is appropriate.

Included below is the patch, for ease of reading, but it's also attached as
a patch (taken against trunk today).

Sean
===========

Index: Doc/tutorial/controlflow.rst
===================================================================
--- Doc/tutorial/controlflow.rst	(revision 67072)
+++ Doc/tutorial/controlflow.rst	(working copy)
@@ -166,7 +166,36 @@
    ...     pass  # Busy-wait for keyboard interrupt (Ctrl+C)
    ... 
 
+This is commonly used for creating minimal classes like with exceptions, or
+for skipping unwanted exceptions::
 
+   >>> class ParserError(Exception):
+   ...     pass
+   ... 
+   >>> try:
+   ...     import audioop
+   ... except ImportError:
+   ...     pass
+   ... 
+
+Another place it can be used is as a place-holder for a function or
+conditional body when you are working on new code, allowing you to keep
+thinking at a more abstract level.  However, as :keyword:`pass` is silently
+ignored, a better choice may be to raise a :exc:`NotImplementedError`
+exception::
+
+   >>> def initlog(*args):
+   ...     raise NotImplementedError   # Open logfile if not already open
+   ...     if not logfp:
+   ...         raise NotImplementedError  # Set up dummy log back-end
+   ...     raise NotImplementedError  # Call log initialization handler
+   ... 
+
+If :keyword:`pass` were used here and you later ran tests, they may fail
+without indicating why.  Using :exc:`NotImplementedError` causes this code
+to raise an exception, allowing you to tell exactly where code that you
+need to complete is.
+
 .. _tut-functions:
 
 Defining Functions
msg75457 - (view) Author: David W. Lambert (LambertDW) Date: 2008-11-02 23:23
I'd change the exceptions, replace comment with string.
Instead of

raise NotImplementedError  # Set up dummy log back-end

write

raise NotImplementedError('Set up dummy log back-end')
msg75461 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2008-11-03 04:38
I originally had that, but then I realized that if I make it a comment I
can then do something like "dw." on that line when I start filling in
the code to keep the comment around.  Because I find that when I fill in
the high level comments they make nice comments to keep around for that
code block.

I'm open to the exception string, but I deliberately switched from the
string to comment.

Sean
msg75538 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2008-11-06 00:11
I'm attaching a new version here, which addresses the object call type
comments.
msg75578 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-11-06 18:49
Committed in r67123. Thanks!
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48497
2008-11-06 18:49:42georg.brandlsetstatus: open -> closed
keywords: patch, patch, easy, needs review
resolution: accepted
messages: + msg75578
nosy: + georg.brandl
2008-11-06 00:11:05jafosetkeywords: patch, patch, easy, needs review
files: + pass-examples-2.patch
messages: + msg75538
2008-11-03 04:38:41jafosetkeywords: patch, patch, easy, needs review
messages: + msg75461
2008-11-02 23:23:47LambertDWsetnosy: + LambertDW
messages: + msg75457
2008-11-02 02:16:02jafocreate