Skip to content

Consistent use of "return None" in functions #399

Description

@gvanrossum

Could you add this rule to pep8? I just updated the official PEP 8 to include this (I'm surprised it wasn't already there):

  • 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)

https://mail.python.org/pipermail/python-dev/2015-April/139054.html

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions