Python Forum
Returning True or False vs. True or None
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning True or False vs. True or None
#3
I think it's generally best to use the first version and explicitly return False. It's often useful to return True, False, or None, with None representing a situation where the truth value could not be determined for some reason. Explicitly return False makes your True/False functions consistent with that usage.

However, I often use a third case: just return condition. If the condition has a boolean operator, just return it:

def five_or_less(x):
     return x <= 5
But perhaps you were only concerned with conditions without a boolean operator.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: Returning True or False vs. True or None - by ichabod801 - Apr-04-2019, 02:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Boolean: Anybody know why is this True? LilD 8 1,917 Feb-06-2022, 09:15 AM
Last Post: LilD
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,144 Jan-09-2022, 02:39 AM
Last Post: Python84
  Question on True expressions mgreen2251660 2 1,955 Jun-25-2021, 04:49 PM
Last Post: Yoriz
  pathlib destpath.exists() true even file does not exist NaN 9 4,740 Dec-01-2020, 12:43 PM
Last Post: NaN
  While True loop help Nickd12 2 2,009 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  path.exists returning True when it shouldn't natha18 0 1,518 Sep-21-2020, 01:04 PM
Last Post: natha18
  while True error Nickd12 2 2,091 Sep-13-2020, 06:29 AM
Last Post: Nickd12
  Recursive function returns None, when True is expected akar 0 3,411 Sep-07-2020, 07:58 PM
Last Post: akar
  isinstance() always return true for object type check Yoki91 2 2,594 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Is using while True loop good? escape_freedom13 5 3,594 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020