What is the correct way to show an operation has failed in python. In this code what should the return values be?
def compare(y,x):
if y == x:
return 'true'
return 'false'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Python has literal values
TrueandFalsewhich you can use. However, it’s rarely necessary to use them explicitly, as comparison operations will return one or the other value in most cases. For instance, you could redo your function to be:It’s also worth noting that non-Boolean values can be considered “true” or “false”, if necessary. The “falsy” values are
None,0and all empty containers (such as the empty string'', the empty list[], the empty tuple(), the empty dictionary{}and so on). Everything else is “truthy” by default, including all instances of most kinds of objects.Custom classes can have a boolean conversion defined by implementing the magical method
__nonzero__(which is renamed to__bool__in Python 3). If it doesn’t exist, Python will check for__len__, and if that doesn’t exist give up and assume all instances are true.