Python will always return None, if no return is specified at the point of exiting the function call. Your options are:

  • return something else if the condition is not met.
  • ignore the function if it returns None

option 1 (return something else when the condition isn't met):

 def html_check(td, text):
     if td.text == text:
        value = td.find_next('td').text
        return value
     return "no value found"

option 2 (ignores the function if None is returned):

if html_check(td, 'section'):
     # do things
Answer from Christian W. on Stack Overflow
🌐
Reddit
reddit.com › r/python › return none or not to return none
r/Python on Reddit: return None or not to return None
June 7, 2013 -

Several weeks ago a was pursuing some of the posts here on python and stumbled on an article about bad programming habits. I cant find the article again for reference but right at the top was a heady warning to avoid returning a meaningful None. AT the time I thought, 'well thats just droll and I never do that anyway', but as it turns out I do it all the time. If the function finds the correct thing then return the correct thing otherwise just end the function and python will return None for you.

So do you guys do the same thing? Whats wrong with returning None in the first place? What strategies do you use to avoid returning None?

🌐
Better Programming
betterprogramming.pub › please-stop-returning-none-from-python-functions-ff98e492b4b0
Stop Returning None From Python Functions | by Imran Ali | Better ...
August 10, 2023 - Stop Returning None From Python Functions Why? Because it’s error-prone and confusing to the caller Often, when reading Python code, you’ll come across utility functions that will return None …
🌐
Reddit
reddit.com › r/learnpython › how to have python return nothing instead of none?
r/learnpython on Reddit: How to have Python return nothing instead of None?
February 3, 2022 -

Python and I are NOT getting along today.

I have the script below, which takes arguments like username and fromdate and passes it into the appropriate place in the URL (the link variable). Now, if the user just inputs username and doesn't input fromdate, Python will return None instead of nothing. Why do you do this, Python!? I never asked you to!

How can I avoid this monstrosity?

Command

main.py  -u jacksfilms

Script

import requests, re, argparse

parser = argparse.ArgumentParser()
parser.add_argument('-u','--username', required=False)
parser.add_argument('-from','--fromdate', required=False)
parser.add_argument('-to','--todate', required=False)
args = vars(parser.parse_args())
username = args['username']
fromdate = args['fromdate']
todate = args['todate']

link = "https://web.archive.org/cdx/search/cdx?url=twitter.com/{}/status&matchType=prefix&from={}&to={}".format(username,fromdate,todate)
data = []

c = requests.get(link).text
urls = re.findall(r'https?://[^\s<>"]+[|www\.^\s<>"]+', c)

for i, url in enumerate (urls):
    data.append(f"{i}: {url}\n")
    
print(data)
🌐
Finxter
blog.finxter.com › python-function-return-none-without-return-statement
Python Function Return None Without Return Statement – Be on the Right Side of Change
The Python return keyword allows you to define the return value of a function (e.g., return X). If you don’t explicitly set a return value or you omit the return statement, Python will implicitly return the following default value: None.
🌐
Real Python
realpython.com › python-return-statement
The Python return Statement: Usage and Best Practices – Real Python
June 14, 2024 - This can cause subtle bugs that can be difficult for a beginning Python developer to understand and debug. You can avoid this problem by writing the return statement immediately after the header of the function.
🌐
YouTube
youtube.com › watch
How to stop Python function from returning None - YouTube
This tutorial explains about various situations where a Python function returns None as output. And also examples show, how to stop python function from retu...
Published   June 12, 2023
Find elsewhere
🌐
Codecademy
codecademy.com › forum_questions › 54f64ad795e3788c240004cc
17/19 code looks OK but return returns "None". | Codecademy
s = "yes" def shut_down(s): if s == "yes": return "Shutting down" elif s == "no": return "Shutting dow...
🌐
Medium
medium.com › @battesti › dont-be-afraid-of-none-in-python-it-s-the-right-way-8f950a310599
Don’t be afraid of None in Python. It’s the right way ! | by Marcu-Andria Battesti | Medium
February 11, 2025 - This makes it clear that there’s no valid result rather than returning a misleading default value. Since None is a singleton in Python, the correct way to check for it is using is: ... if value is None: # Correct print("Value is missing") if value == None: # Incorrect print("Avoid using '==' to compare with None")
🌐
Python.org
discuss.python.org › python help
'None' keeps appearing in the output - How to remove it? - Python Help - Discussions on Python.org
March 29, 2023 - This is an assignment, when I run the code it runs properly but keeps putting ‘None’ at the end. Can anyone help me? def option_A(): fBondValue = float(input("\nEnter face value of bond: ")) fIntr = float(input…
🌐
Real Python
realpython.com › lessons › returning-none-explicitly
Returning None Explicitly (Video) – Real Python
A word of caution: many Python programmers come from other programming languages, so not having a return statement at all for a function might be confusing for those who are used to referring to such subprograms as procedures. 02:38 This might be an issue to consider in thinking about the long-term maintainability of your project. Next, we’ll look at two other best practices involving remembering the return value and avoiding ...
Published   August 10, 2021
🌐
Julianhysi
julianhysi.com › post › 4
Julian's Blog - Bare return vs return None vs no return
This leaves the Python developer with a few options: - don't do anything (the function will implicitly return None) ... Now, all of these options will return None, so there's no difference in behavior. It's purely a choice of coding style, but that doesn't mean it's irrelevant.
🌐
Python.org
discuss.python.org › python help
How to remove "None" from output - Python Help - Discussions on Python.org
August 14, 2022 - This is an assignment. I have everything correct EXCEPT the last thing it prints is None. Test input “This is awesome” Test input “is” Test input (any word not in text) How do I get rid of the None that prints? Thanks def search(text, word): if word in text: print('Word found') else: print('Missing ') text = input() word = input() print(search(text, word))
🌐
AskPython
askpython.com › home › python return statement
Python return Statement - AskPython
May 11, 2023 - So, either you have used a return statement with no value, or there is no return statement, and the function returns None. A function can have multiple returns, the one that satisfies the condition will be executed first and the function will exit. ... def type_of_int(i): if i % 2 == 0: return 'even' else: return 'odd' result = type_of_int(7) print(result) ... Unlike other programming languages, Python functions are not restricted to returning a single type of value.
🌐
Python.org
discuss.python.org › python help
Print returning none - Python Help - Discussions on Python.org
November 8, 2022 - Hi everyone, I started self learning python and I have no background of programming before. Trying to self study from youtube. I’m practicing if else, while and so on but for some reason the print is returning none. …