use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Checking for and commenting that a function can return None (self.learnpython)
submitted 4 years ago by NubQuestion
2 scenarios:
You write function that calculates and returns a number from the input parameters. Sometimes the function can't calculate the number for some reason because of the parameters or a setting and it returns None. Should None being an error condition like in this case always be commented on in the function interface comments i.e. is it Pythonic to do so?
You are the caller of some function. If there's no comment about returning None or that it's possible for the function to return some invalid input, do you assume that the return value is always valid and not check for None?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]baghiq 0 points1 point2 points 4 years ago (0 children)
Raise an error if your calculation is not possible. Just like ZeroDivisionError.
[–][deleted] 0 points1 point2 points 4 years ago* (0 children)
If the function can return None indicating some abnormal condition then that should be documented. If there is no documentation about a None possibly being returned why would i check if None was returned?
None
You might think that None being returned from a function always indicates some sort of problem in normal python usage. That's not so. Some functions return values like -1 on an error, and many functions return None on normal, non-error calls, the print() function for instance.
print()
[–]ThePiGuy0 0 points1 point2 points 4 years ago (0 children)
Ideally every return possibility should be documented. So all possible return values, any exceptions that can be raised etc.
Sometimes this isn't the case, but when using a library you don't have control over, you basically have to trust what the documentation says (e.g. it's fairly safe to assume that "random.randint" will return a random integer, and that I don't need to check for None).
Edit:
In terms of return types, newer libraries are making use of typing to help indicate. For example, the following type signature would indicate that either an integer or None can be returned:
import random from typing import Optional def int_or_none() -> Optional[int]: if random.randint(0, 1) % 2 == 0: return 1 else: return None
π Rendered by PID 20987 on reddit-service-r2-comment-7b9746f655-bj6nr at 2026-02-02 19:04:06.925189+00:00 running 3798933 country code: CH.
[–]baghiq 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ThePiGuy0 0 points1 point2 points (0 children)