[Python-checkins] peps: Describe an old but good rule for consistent return statements.

guido.van.rossum python-checkins at python.org
Tue Apr 7 03:07:23 CEST 2015


https://hg.python.org/peps/rev/dcca553d365d
changeset: 5749:dcca553d365d
user: Guido van Rossum <guido at python.org>
date: Mon Apr 06 18:07:10 2015 -0700
summary:
 Describe an old but good rule for consistent return statements.
files:
 pep-0008.txt | 31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)
diff --git a/pep-0008.txt b/pep-0008.txt
--- a/pep-0008.txt
+++ b/pep-0008.txt
@@ -1151,6 +1151,37 @@
 closing the connection after a transaction. Being explicit is
 important in this case.
 
+- Be consistent in return statements. Either all return statements in
+ a function should return an expression, or none of them should. If
+ any return statement returns an expression, any return statements
+ where no value is returned should explicitly state this as ``return
+ None``, and an explicit return statement should be present at the
+ end of the function (if reachable).
+
+ Yes::
+
+ def foo(x):
+ if x >= 0:
+ return math.sqrt(x)
+ else:
+ return None
+
+ def bar(x):
+ if x < 0:
+ return None
+ return math.sqrt(x)
+
+ No::
+
+ def foo(x):
+ if x >= 0:
+ return math.sqrt(x)
+
+ def bar(x):
+ if x < 0:
+ return
+ return math.sqrt(x)
+
 - Use string methods instead of the string module.
 
 String methods are always much faster and share the same API with
-- 
Repository URL: https://hg.python.org/peps


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /