[Python-checkins] peps: Update PEP 463 with Chris Angelico's latest version, with slightly fixed

thomas.wouters python-checkins at python.org
Thu Feb 27 23:32:14 CET 2014


http://hg.python.org/peps/rev/2503db6ec908
changeset:   5387:2503db6ec908
user:        Thomas Wouters <thomas at python.org>
date:        Thu Feb 27 23:32:04 2014 +0100
summary:
  Update PEP 463 with Chris Angelico's latest version, with slightly fixed
formatting.

files:
  pep-0463.txt |  44 +++++++++++++++++++++++++++++++++++++++-
  1 files changed, 43 insertions(+), 1 deletions(-)


diff --git a/pep-0463.txt b/pep-0463.txt
--- a/pep-0463.txt
+++ b/pep-0463.txt
@@ -43,6 +43,34 @@
 
 * statistics.mean(data) - no way to handle an empty iterator
 
+Had this facility existed early in Python's history, there would have been
+no need to create dict.get() and related methods; the one obvious way to
+handle an absent key would be to respond to the exception.  One method is
+written which signals the absence in one way, and one consistent technique
+is used to respond to the absence.  Instead, we have dict.get(), and as of
+Python 3.4, we also have min(... default=default), and myriad others.  We
+have a LBYL syntax for testing inside an expression, but there is currently
+no EAFP notation; compare the following::
+
+    # LBYL:
+    if key in dic:
+        process(dic[key])
+    else:
+        process(None)
+    # As an expression:
+    process(dic[key] if key in dic else None)
+
+    # EAFP:
+    try:
+        process(dic[key])
+    except KeyError:
+        process(None)
+    # As an expression:
+    process(dic[key] except KeyError: None)
+
+Python generally recommends the EAFP policy, but must then proliferate
+utility functions like dic.get(key,None) to enable this.
+
 
 Rationale
 =========
@@ -338,6 +366,20 @@
     except KeyError:
         u = tarinfo.uid
 
+Look up an attribute, falling back on a default::
+
+    mode = (f.mode except AttributeError: 'rb')
+
+    # Lib/aifc.py:882:
+    if hasattr(f, 'mode'):
+        mode = f.mode
+    else:
+        mode = 'rb'
+
+    return (sys._getframe(1) except AttributeError: None)
+    # Lib/inspect.py:1350:
+    return sys._getframe(1) if hasattr(sys, "_getframe") else None
+
 Perform some lengthy calculations in EAFP mode, handling division by
 zero as a sort of sticky NaN::
 
@@ -616,7 +658,7 @@
 it would be with the statement form, and as its syntax is a point on
 which consensus has not been reached, the entire feature is deferred.
 
-Multiple 'except' keywords can be used, and they will all catch
+Multiple 'except' keywords could be used, and they will all catch
 exceptions raised in the original expression (only)::
 
     # Will catch any of the listed exceptions thrown by expr;

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list