CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Python – Truth Value Testing for Class objects

We have discussed Truth Value Testing in our previous Article, “Python – Truth Value Testing“; where we have discussed that Class objects are, by default considered True for Truth Value Testing.

Through this article, we will discuss the Truth Value Testing for Class objects.

Truth Value Testing for Class objects

By default, Truth Value Testing for Class objects is True. Let’s see how this works. Below is the example, to verify the Truth Value Testing for Class objects.

>>> class Sample: pass
>>> obj = Sample()
>>> if obj:
...    True
... else:
...    False
... 
True

This shows that by default, Truth Value for Class objects is True. But when you try with == operator; the result shows something different. Here is the example code;

>>> obj == True
>>> False

Why the testing shows a False value? This is because == operator verifies whether two operands have the same value. As both the values are different (obj value is NOT equal to True), it returns the Truth Value False.

I know you have these questions in your mind: Then why True == 1 returns True as the Truth Value? And what is the meaning of this statement “By default, Truth Value Testing for Class objects is True”?

As I mentioned above, when you use == operator it verifies the equality of the values of the two operands. In order to implement the Truth Value Testing using == operator; you need to implement the __eq__ method for your class. You need to place the logic inside __eq__ method for the Truth Value Testing.

Here is the modified version of the above class.

>>> class Sample:
...    def __eq__(self, arg):
...       return (bool(self) == bool(arg))
... 
>>> obj = Sample()
>>> obj == True
True

Observe that, now obj == True statement returns the Truth Value True. And also important that, you MUST NOT use return (self == arg) statement within the __eq__ method. Otherwise, Python will throw the below Error message; because, you are attempting to call the __eq__ method recursively.

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in __eq__
  File "", line 3, in __eq__
  File "", line 3, in __eq__
  [Previous line repeated 495 more times]
RecursionError: maximum recursion depth exceeded

Hence in the above example, we have compared the equality check for Truth Value Testing using bool.

To answer the second question; what is the meaning of this statement “By default, Truth Value Testing for Class objects is True”?; Yes, by default Truth Value Testing returns True for Class objects. This was proved in the first example. But when you use == operator; you need to take special care in your Class.

Basically, Python uses boolean checks for Truth Value Testing. When you use if obj: statement; Python internally checks this as bool(obj); which returns True as by default, an object considers True in Truth Value Testing.

/Shijit/

Python – Truth Value Testing for Class objects

One thought on “Python – Truth Value Testing for Class objects

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top