1

What is the meaning of having the facility to compare Booleans and Strings?. Why does the result "True" is given for the expression "a" > True or "a" > False.What I want to know is what use is there or What do the developers of the language expect for the users of Python by providing such a feature?

Varuna
  • 1,343
  • 3
  • 15
  • 30
  • You means `"a" == True` , two `=`, btw `v = "a"` `bool(v) == True` is more meaningful to test whether `v` points to empty string or not – Grijesh Chauhan Oct 28 '13 at 14:34
  • 1
    Your question is rather unclear. Perhaps some code samples would help. – Games Brainiac Oct 28 '13 at 14:34
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Show some effort. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Inbar Rose Oct 28 '13 at 14:34
  • The comparisons you posted are `False` for me (using Python 2.7.2). – Robin Krahl Oct 28 '13 at 14:36
  • Above question was asked by me in this way "What is the meaning of having the facility to compare Booleans and Strings?.Why does the result "True" is given for the expression "a" > True or "a" > False".and it has been edited – Varuna Oct 28 '13 at 14:40
  • Also, `"a" > 2` yields `True`... – DJG Oct 28 '13 at 14:42
  • Also, see http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int – DJG Oct 28 '13 at 14:43
  • I think your question has become a duplicate of http://stackoverflow.com/questions/2384078/why-is-0-true-in-python – DJG Oct 28 '13 at 14:49

5 Answers5

3

Since Python is a dynamically typed language, the interpreter can't know that it is comparing a Boolean and a String until runtime. In other words, given:

if "string" == True: 
    print "Doesn't print"

When the interpreter sees this code, there are two options:

  1. Make "string" == True evaluate to False (or "string" == False)

  2. Throw a TypeError exception

And I really can't see a good reason to go with 2, because if two values are of different types, then they are clearly unequal, so the code in the if statement just shouldn't be run.

In terms of > and < between different types, see Why is ''>0 True in Python?: from that answer:

The original design motivation for allowing order-comparisons of arbitrary objects was to allow sorting of heterogenous lists -- usefully, that would put all strings next to each other in alphabetical order, and all numbers next to each other in numerical order, although which of the two blocks came first was not guaranteed by the language. For example, this allowed getting only unique items in any list (even one with non-hashable items) in O(N log N) worst-case time

See the link for the rest.

Community
  • 1
  • 1
DJG
  • 6,413
  • 4
  • 30
  • 51
2

Containers in python, including lists, tuples, dictionaries, sets and strings(which contain characters), are always True when they contain one or more elements and always False when they are empty.

Basically, this is used to check if the container has any elements before doing something. Example:

food_in_the_plate = []
if food_in_the_plate:
    eat(food_in_the_plate)
else:
    grab_food()
OdraEncoded
  • 3,064
  • 3
  • 20
  • 31
1

Yes, it does muddle things a little. But it can be useful. For example

x = {"a": "AA", "b": "BB"}
got_a = x.get("a")
print got_a
>> "AA"

got_b = x.get("b")
print got_b
>> "BB"

got_c = x.get("c")
print got_c
>> None

You can then test if the values are None or not:

if got_a:
  print "Got A"

if got_c:
  print "Got C"

This will print "Got A" but not "Got C" because the value of got_c is None.

There are lots of situations like this where evaluating non-boolean expressions as booleans means shorter code.

Joe
  • 46,419
  • 33
  • 155
  • 245
1
if p == "" or p == None or p == False:
    p = 'This language could be more Pythonic'

P = P or 'Nicely done'
dansalmo
  • 11,506
  • 5
  • 58
  • 53
  • Worth adding the counter-example if you actually care about the difference between empty string, `None` and `False`. – Joe Oct 28 '13 at 14:50
0

No matter what you compare a string with a boolean to (using ==), you will always get a false:

>>> 'a' == True
False
>>> 'a' == False
False
>>> '' == False
False
>>> '' == True
False
>>> 'hello' == True
False
>>> 'True' == True
False
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199