Describe an old but good rule for consistent return statements.
This commit is contained in:
parent
bab89a734b
commit
f80066c3fb
31
pep-0008.txt
31
pep-0008.txt
|
@ -1151,6 +1151,37 @@ Programming Recommendations
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue