{"id":160,"date":"2024-01-25T14:01:45","date_gmt":"2024-01-25T14:01:45","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=160"},"modified":"2024-01-25T14:01:46","modified_gmt":"2024-01-25T14:01:46","slug":"python-__bool__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__bool__\/","title":{"rendered":"Python __bool__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to implement the Python&nbsp;<code>__bool__<\/code>&nbsp;method to return boolean values for objects of a custom class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python __bool__ method<\/h2>\n\n\n\n<p>An object of a custom\u00a0class\u00a0is associated with a boolean value. By default, it evaluates to\u00a0<code>True<\/code>. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name, age): self.name = name self.age = age if __name__ == '__main__': person = Person('John', 25)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, we define the&nbsp;<code>Person<\/code>&nbsp;class, instantiate an object, and show its boolean value. As expected, the&nbsp;<code>person<\/code>&nbsp;object is&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>To override this default behavior, you implement the&nbsp;<code>__bool__<\/code>&nbsp;special method. The&nbsp;<code>__bool__<\/code>&nbsp;method must return a boolean value,&nbsp;<code>True<\/code>&nbsp;or&nbsp;<code>False<\/code>.<\/p>\n\n\n\n<p>For example, suppose that you want the\u00a0<code>person<\/code>\u00a0object to evaluate\u00a0<code>False<\/code>\u00a0if the age of a person is under 18 or above 65:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name, age): self.name = name self.age = age def __bool__(self): if self.age &lt; 18 or self.age > 65: return False return True if __name__ == '__main__': person = Person('Jane', 16) print(bool(person)) <em># False<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>__bool__<\/code>&nbsp;method returns&nbsp;<code>False<\/code>&nbsp;if the age is less than 18 or greater than 65. Otherwise, it returns&nbsp;<code>True<\/code>. The person object has the age value of 16 therefore it returns False in this case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The __len__ method<\/h2>\n\n\n\n<p>If a custom class doesn\u2019t have the&nbsp;<code>__bool__<\/code>&nbsp;method, Python will look for the&nbsp;<code>__len__()<\/code>&nbsp;method. If the&nbsp;<code>__len__<\/code>&nbsp;is zero, the object is&nbsp;<code>False<\/code>. Otherwise, it\u2019s&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>If a class doesn\u2019t implement the&nbsp;<code>__bool__<\/code>&nbsp;and&nbsp;<code>__len__<\/code>&nbsp;methods, the objects of the class will evaluate to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>The following defines a\u00a0<code>Payroll<\/code>\u00a0class that doesn\u2019t implement\u00a0<code>__bool__<\/code>\u00a0but the\u00a0<code>__len__<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Payroll: def __init__(self, length): self.length = length def __len__(self): print('len was called...') return self.length if __name__ == '__main__': payroll = Payroll(0) print(bool(payroll)) <em># False<\/em> payroll.length = 10 print(bool(payroll)) <em># True<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since the&nbsp;<code>Payroll<\/code>&nbsp;class doesn\u2019t override the&nbsp;<code>__bool__<\/code>&nbsp;method, Python looks for the&nbsp;<code>__len__<\/code>&nbsp;method when evaluating the Payroll\u2019s objects to a boolean value.<\/p>\n\n\n\n<p>In the following example payroll\u2019s\u00a0<code>__len__<\/code>\u00a0returns 0, which is\u00a0<code>False<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>payroll = Payroll(0) print(bool(payroll)) <em># False<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>However, the following example\u00a0<code>__len__<\/code>\u00a0returns 10 which is\u00a0<code>True<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>payroll.length = 10 print(bool(payroll)) <em># True<\/em><\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to implement the Python&nbsp;__bool__&nbsp;method to return boolean values for objects of a custom class. Introduction to the Python __bool__ method An object of a custom\u00a0class\u00a0is associated with a boolean value. By default, it evaluates to\u00a0True. For example: In this example, we define the&nbsp;Person&nbsp;class, instantiate an object, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-2-special-methods"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":161,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/160\/revisions\/161"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}