Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs don't explicitly describe an implicit return None in user-defined function #92753

Open
mbottini opened this issue May 12, 2022 · 2 comments
Open
Labels
docs

Comments

@mbottini
Copy link

@mbottini mbottini commented May 12, 2022

Documentation

Empirically, it is the case that when execution reaches the end of a user-defined function body without encountering a return statement, the function returns None.

def foo():
    pass
    # implicit `return None`

And the docs hint at this in places! From here, in 6.3.4, emphasis added:

A call always returns some value, possibly None, unless it raises an exception. How this value is computed depends on the type of the callable object.

Later, in User-Defined Functions:

When the code block executes a return statement, this specifies the return value of the function call.

Additionally, here in 3.2:

[None] is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything.

But the reference doesn't actually come out and explicitly say what happens in the absence of a return statement.

Possible Fix:

Ideally, I'd add one more sentence to the User-Defined functions section after the above quote:

If the code block reaches the end of the function body without executing a return statement, the return value is None.

@mbottini mbottini added the docs label May 12, 2022
@sweeneyde
Copy link
Member

@sweeneyde sweeneyde commented May 12, 2022

For what it's worth, this tutorial page already has the following sentences:

In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it’s a built-in name).

The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.

@mbottini
Copy link
Author

@mbottini mbottini commented May 12, 2022

That's fair, although I'd argue that the tutorial should be an informal exploration of behavior that is rigorously specified in the reference. It's totally fine for the tutorial to be explicit too, but it's strange to me that the formal language reference (which is what implementors should be targeting) is less rigorous than the tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs
Projects
None yet
Development

No branches or pull requests

2 participants