Skip to content Skip to sidebar Skip to footer

Understanding The Truthiness Of Strings

I understand that Python built-in types have a 'truthiness' value, and the empty string is considered False, while any non-empty string is considered True. This makes sense I can c

Solution 1:

See the truth value testing and comparisons sections of the documentation, excerpted below.

In a nutshell, most things are truthy by default, which is why bool("dog") is true. The == operator compares two objects for equality, as opposed to comparing their truthinesses, as I assume you had expected.

4.1. Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object.

Here are most of the built-in objects considered false:

  • constants defined to be false: None and False
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

4.3. Comparisons

Objects of different types, except different numeric types, never compare equal.

...

Non-identical instances of a class normally compare as non-equal unless the class defines the __eq__() method.

Solution 2:

The basics

I believe your confusion might come from comparing Python to languages such as JavaScript where there is a == and a === operator. Python does not work this way.

In Python the only way to compare for equality is with == and this compares both value and type.

Thus if you compare True == "dog", then the expression is immediately False because the types bool and str are not types that can be compared.

Although, note that it does not mean that there are no types that are comparable between themselves. Examples are set and frozenset:

frozenset({1,2,3}) == {1,2,3} # True

Or simply int and float

1 == 1.0# True

This is the behaviour for most built-in types.

The classy part

In the case where you define your own types, i.e. when you define classes, you can write the __eq__ which is called when you compare a class object to another value.

By example you could do this (which by the way was pointed out as a terrible idea in the comments, you should not inherit built-in types).

classWeirdString(str):
    def__eq__(self, other):
        returnstr(self) == str(other) orbool(self) == bool(other)

s = WeirdString("dog")
s == True# True

In the case where you do not define __eq__, then Python fall back on comparing whether the objects are the same object with is.

Solution 3:

When you compare "dog" == True, you are also comparing the type of these objects and not just their boolean value.

Now as True has a type bool and "dog" has a type str, they are not equivalent according to the == operator, irrespective of their boolean values being equal.

Note: Both the object's type,boolean values are being checked here.

Post a Comment for "Understanding The Truthiness Of Strings"