>>> if '' is not None:
... print'23333'
...
23333
I think (not None) is True and ('') is False so why it running print?
is and is not test for object identity, i.e., will test if '' and None are the same object, which they are not, so the test returns True in your case.
From the Python documentation:
The operators is and is not test for object identity: x is y is true
if and only if x and y are the same object. x is not y yields the
inverse truth value.
To put it another way, although '' and None have the same "truthiness", that is they both evaluate to False if you do bool(None) or bool(''), they to do not refer to the same object.
is not is a single operator, equal to the negation of is. Since '' is None is false, '' is not None is true.
But since is tests identity, not equality, '' is (not None) still won't do what you want.
Related
I just stumbled across this and I couldn't find a sufficient answer:
x = ""
Why then is:
x == True
False
x == False
False
x != True
True
x != False
True
Am I supposed to conclude that x is neither True nor False?
to check if x is True of False:
bool("")
> False
bool("x")
> True
for details on the semantics of is and == see this question
Am I supposed to conclude that x is neither True nor False?
That's right. x is neither True nor False, it is "". The differences start with the type:
>>> print(type(""), type("x"), type(True), type(False))
builtins.str, builtins.str, builtins.bool, builtins.bool
Python is a highly object oriented language. Hence, strings are objects. The nice thing with python is that they can have a boolean representation for if x: print("yes"), e. g.. For strings this representation is len(x)!=0.
In a Boolean context, null / empty strings are false (Falsy). If you use
testString = ""
if not testString:
print("NULL String")
else:
print(testString)
As snakecharmerb said, if you pass the string to the bool() function it will return True or False based
>>> testString = ""
>>> bool(testString)
False
>>> testString = "Not an empty string"
>>> bool(testString)
True
See this doc on Truth Value Testing to learn more about this:
Python 2:
https://docs.python.org/2/library/stdtypes.html#truth-value-testing
Python 3:
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
In python '==' tests for equality. The empty string is not equal to True, so the result of your comparison is False.
You can determine the 'truthiness' of the empty string by passing it to the bool function:
>>> x = ''
>>> bool(x)
False
Why does assertFalse succeed on None?
import unittest
class TestNoneIsFalse(unittest.TestCase):
def test_none_is_false(self):
self.assertFalse(None)
Results:
> python -m unittest temp
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
It seems as if this behaviour invites errors where a function does not always return a value. For example:
def is_lower_than_5(x):
if x < 5:
return True
elif x > 5:
return False
....
def test_5_is_not_lower_than_5(self):
self.assertFalse(is_lower_than_5(5))
The above test would pass even though it should fail. It is missing an error in the code that should be caught.
How should we assert that the value is literally False and not merely false in a boolean context? e.g.
self.assertEquals(False, None) # assert fails. good!
None is falsy, as well as 0, "", [], ...
assertFalse does not check whether the given value is False by identity. This behavior is consistent with the if statement:
if not None:
print('falsy value!')
Similarly, assertTrue does not check whether a value is True, and as such values like 1, "abc", [1, 2, 3] pass the test. See Truth Value Testing for more information.
This behavior is also explicitly documented:
assertTrue(expr, msg=None)
assertFalse(expr, msg=None)
Test that expr is true (or false).
Note that this is equivalent to bool(expr) is True and not to expr is True
If you really want to be sure that a value is True or False, use assertIs.
Your case is actually pointed out in the documentation:
Note that this is equivalent to bool(expr) is True and not to expr is
True (use assertIs(expr, True) for the latter).
Source: https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertFalse
Python default return value for functions is None.
Python also implements Duck typing, so some values are threated as falsey, those are
empty string
integer zero
boolean False
empty list
empty tuple
So yes, you should implement it as self.assertEquals(False, None) if you want to explicitly check the False boolean value. This is not a good idea, in python you can just do
if my_value:
print 'truthy value'
else:
print 'falsey value'
You could design your test cases using boundary-value analysis to check your corner cases.
This question already has answers here:
Is short-circuiting in assignment statements considered good style?
(2 answers)
Closed 7 years ago.
I was reading through an API's documentation when I came across a curious little set of statements:
self.use_ssl = kwargs.get('use_ssl', True)
self.scheme = self.use_ssl and 'https://' or 'http://'
After doing some personal testing, I found that if self.use_ssl was set to True, self.scheme would be set to use HTTPS, and HTTP if self.use_ssl was False. Awesomely pythonic, and I'll definitely be stealing this.
Can somebody explain exactly how this works?
In python, an empty string is equivalent to False, a non empty string is equivalent to True
>>> bool('')
False
>>> bool('foo')
True
The behavior of a boolean expression is described in the python 2 documentation, and is the same for python 3.
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
That's why you get the string 'https://' or 'http://' depending on the value of 'self.use_ssl'
Some examples, from the python console:
>>> True or ''
True
>>> True or 'foo'
True
>>> False or ''
''
>>> False or 'foo'
'foo'
>>> '' or True
True
>>> '' or False
False
>>> 'bar' or True
'bar'
>>> 'bar' or False
'bar'
>>> True and ''
''
>>> True and 'foo'
'foo'
>>> False and ''
False
>>> False and 'foo'
False
>>> '' and True
''
>>> '' and False
''
>>> 'bar' and True
True
>>> 'bar' and False
False
You can always convert a boolean expression to a real boolean value using bool()
>>> 1 and 'bar'
'bar'
>>> bool(1 and 'bar')
True
This trick, a and b or c, only works when b itself is a "truthy" value. Consider True and "" or "foo". You might expect it to produce the empty string, but it will produce foo, because True and "" results in the empty string, which is considered False when evaluating "" or "foo". The correct way would be to wrap b and c in a list to guarantee that the second argument to and is truthy (and that the overall result is a list in either case), then extract the first value of that list:
(a and [b] or [c])[0]
To avoid the clunkiness (and inefficiency) of creating and indexing a temporary list, Python introduced the conditional expression:
b if a else c
which does not rely on b having any particular boolean value.
it works like this:
when python interapt this it first checks the self.use_ssl if it is True
it continues in the AND chain to see if the next statement is True.
since the next statement is a string (and not an empty one) it is True
so there is no need to continue to the or since this statement is definetly True already, so the last value is used witch was 'https'
if use_ssl was false then there is no need to evaluate the and part of the condition since the first part is already false so python "skip" it and continue to the or part to check if that is True, since it is again a non empty string it is true, and again the last value used is "returned"
I did several Boolean Comparisons:
>>> (True or False) is True
True
>>> (True or False) == True
True
It sounds like == and is are interchangeable for Boolean-values.
Sometimes it's more clear to use is
I want to know that:
Are True and False pre-allocated in python?
Is bool(var) always return the same True(or False) with the pre-allocated True(or False)?
Is it safe to replace == with is to compare Boolean-values?
It's not about Best-Practice.
I just want to know the Truth.
You probably shouldn't ever need to compare booleans. If you are doing something like:
if some_bool == True:
...
...just change it to:
if some_bool:
...
No is or == needed.
As commenters have pointed out, there are valid reasons to compare booleans. If both booleans are unknown and you want to know if one is equal to the other, you should use == or != rather than is or is not (the reason is explained below). Note that this is logically equivalent to xnor and xor respectively, which don't exist as logical operators in Python.
Internally, there should only ever be two boolean literal objects (see also the C API), and bool(x) is True should be True if bool(x) == True for any Python program. Two caveats:
This does not mean that x is True if x == True, however (eg. x = 1).
This is true for the usual implementation of Python (CPython) but might not be true in other implementations. Hence == is a more reliable comparison.
Watch out for what else you may be comparing.
>>> 1 == True
True
>>> 1 is True
False
True and False will have stable object ids for their lifetime in your python instance.
>>> id(True)
4296106928
>>> id(True)
4296106928
is compares the id of an object
EDIT: adding or
Since OP is using or in question it may be worth pointing this out.
or that evaluates True: returns the first 'True' object.
>>> 1 or True
1
>>> 'a' or True
'a'
>>> True or 1
True
or that evaluates False: returns the last 'False' object
>>> False or ''
''
>>> '' or False
False
and that evaluates to True: returns the last 'True' object
>>> True and 1
1
>>> 1 and True
True
and that evaluates to False: returns the first 'False' object
>>> '' and False
''
>>> False and ''
False
This is an important python idiom and it allows concise and compact code for dealing with boolean logic over regular python objects.
>>> bool([])
False
>>> bool([0])
True
>>> bool({})
False
>>> bool({False: False})
True
>>> bool(0)
False
>>> bool(-1)
True
>>> bool('False')
True
>>> bool('')
False
Basically 'empty' objects are False, 'non empty' are True.
Combining this with #detly's and the other answers should provide some insight into how to use if and bools in python.
Yes. There are guaranteed to be exactly two bools, True and False:
Class bool cannot be subclassed
further. Its only instances are False
and True.
That means if you know both operands are bool, == and is are equivalent. However, as detly notes, there's usually no reason to use either in this case.
It seems that all answers deal with True and False as defined after an interpreter startup. Before booleans became part of Python they were often defined as part of a program. Even now (Python 2.6.6) they are only names that can be pointed to different objects:
>>> True = 1
>>> (2 > 1)
True
>>> (2 > 1) == True
True
>>> (2 > 1) is True
False
If you have to deal with older software, be aware of that.
The == operator tests for equality The is keyword tests for object identity. Whether we are talking about the same object. Note, that more variables may refer to the same object.
== and is are both comparison operators, which would return a boolean value - True or False. True has a numeric value of 1 and False has a numeric value of 0.
The operator == compare the values of two objects and objects compared are most often are the same types (int vs int, float vs float), If you compare objects of different types, then they are unequal. The operator is tests for object identity, 'x is y' is true if both x and y have the same id. That is, they are same objects.
So, when you are comparing if you comparing the return values of same type, use == and if you are comparing if two objects are same (be it boolean or anything else), you can use is.
42 is 42 is True and is same as 42 == 42.
Another reason to compare values using == is that both None and False are “falsy” values. And sometimes it’s useful to use None to mark a value as “not defined” or “no value” while considering True and False values to work with:
def some_function(val = None):
"""This function does an awesome thing."""
if val is None:
# Values was not defined.
elif val == False:
# User passed boolean value.
elif val == True:
# User passed boolean value.
else:
# Quack quack.
Somewhat related question: Python != operation vs “is not”.
It appears that if x is almost like short-hand for the longer if x is not None syntax. Are they functionally identical or are there cases where for a given value of x the two would evaluate differently?
I would assume the behavior should also be identical across Python implementations - but if there are subtle differences it would be great to know.
In the following cases:
test = False
test = ""
test = 0
test = 0.0
test = []
test = ()
test = {}
test = set()
the if test will differ:
if test: #False
if test is not None: #True
This is the case because is tests for identity, meaning
test is not None
is equivalent to
id(test) == id(None) #False
therefore
(test is not None) is (id(test) != id(None)) #True
The former tests trueness, whereas the latter tests for identity with None. Lots of values are false, such as False, 0, '', and None, but only None is None.
x = 0
if x: ... # False
if x is not None: ... # True
if x checks if x is considered as True.
In Python, everything has a boolean value (True/False).
Values that are considered as False:
False, None
0, 0.0, 0j
[], (), {}
''
Other instances that signal to Python that they are empty
Other values are considered as True. For example, [False], ('hello'), 'hello' are considered as True (because they are not empty).
When using if x is not None, you are checking if x is not None, but it can be False or other instances that are considered as False.
>>> x = None
>>> if not x:print x # bool(None) is False
None
>>> if x == None:print x
None
>>> x = False
>>> if not x:print x
False
>>> if x == None:print x
Finally, note that True and False are respectively equal to 1 and 0:
>>> True + 1
2
>>> False + 1
1
>>> range(1, 5)[False]
1
if x:
# Evaluates for any defined non-False value of x
if not x:
# Evaluates for any defined False value of x
if x is None:
# Evaluates for any instances of None
None is its own type, which happens to be False. "if not x" evaluates if x = None, only because None is False.
There aren't any subtle differences that I know of but there are exact methods to test for use for positivity/negativity in exact situations. Mixing them can work in some situations, but can lead to problems if they're not understood.
if x is True:
# Use for checking for literal instances of True
if x is False:
# Use for checking for literal instances of False
if x is None:
# Use for checking for literal instances of None
if x:
# Use for checking for non-negative values
if not x:
# Use for checking for negative values
# 0, "", None, False, [], (), {} are negative, all others are True