{"id":2595,"date":"2021-10-20T01:55:48","date_gmt":"2021-10-20T01:55:48","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2595"},"modified":"2025-03-31T09:01:19","modified_gmt":"2025-03-31T09:01:19","slug":"python-__bool__","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-__bool__\/","title":{"rendered":"Python __bool__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to implement the Python <code>__bool__<\/code> method to return boolean values for objects of a custom class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-__bool__-method'>Introduction to the Python __bool__ method <a href=\"#introduction-to-the-python-__bool__-method\" class=\"anchor\" id=\"introduction-to-the-python-__bool__-method\" title=\"Anchor for Introduction to the Python __bool__ method\">#<\/a><\/h2>\n\n\n\n<p>An object of a custom <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a> is associated with a boolean value. By default, it evaluates to <code>True<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n\nperson = Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-number\">25<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we define the <code>Person<\/code> class, instantiate an object, and show its boolean value. As expected, the <code>person<\/code> object is <code>True<\/code>.<\/p>\n\n\n\n<p>To override this default behavior, you implement the <code>__bool__<\/code> special method. The <code>__bool__<\/code> method must return a boolean value, <code>True<\/code> or <code>False<\/code>.<\/p>\n\n\n\n<p>For example, suppose that you want the <code>person<\/code> object to evaluate  <code>False<\/code> if the age of a person is under 18 or above 65:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name, age)<\/span>:<\/span>\n        self.name = name\n        self.age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__bool__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> self.age &lt; <span class=\"hljs-number\">18<\/span> <span class=\"hljs-keyword\">or<\/span> self.age &gt; <span class=\"hljs-number\">65<\/span>:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">False<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">True<\/span>\n\n\nperson = Person(<span class=\"hljs-string\">'Jane'<\/span>, <span class=\"hljs-number\">16<\/span>)\nprint(bool(person))  <span class=\"hljs-comment\"># False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Y2xhc3MgUGVyc29uOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIG5hbWUsIGFnZSk6CiAgICAgICAgc2VsZi5uYW1lID0gbmFtZQogICAgICAgIHNlbGYuYWdlID0gYWdlCgogICAgZGVmIF9fYm9vbF9fKHNlbGYpOgogICAgICAgIGlmIHNlbGYuYWdlIDwgMTggb3Igc2VsZi5hZ2UgPiA2NToKICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgcmV0dXJuIFRydWUKCgpwZXJzb24gPSBQZXJzb24oJ0phbmUnLCAxNikKcHJpbnQoYm9vbChwZXJzb24pKSAgIyBGYWxzZQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>__bool__<\/code> method returns <code>False<\/code> if the age is less than 18 or greater than 65.  Otherwise, it returns <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\" id='the-__len__-method'>The __len__ method <a href=\"#the-__len__-method\" class=\"anchor\" id=\"the-__len__-method\" title=\"Anchor for The __len__ method\">#<\/a><\/h2>\n\n\n\n<p>If a custom class doesn&#8217;t have the <code>__bool__<\/code> method, Python will look for the <code>__len__()<\/code> method. If the <code>__len__<\/code> is zero, the object is <code>False<\/code>. Otherwise, it&#8217;s <code>True<\/code>.<\/p>\n\n\n\n<p>If a class doesn&#8217;t implement the <code>__bool__<\/code> and <code>__len__<\/code> methods, the objects of the class will evaluate to <code>True<\/code>.<\/p>\n\n\n\n<p>The following defines a <code>Payroll<\/code> class that doesn&#8217;t implement <code>__bool__<\/code> but the <code>__len__<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Payroll<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, length)<\/span>:<\/span>\n        self.length = length\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        print(<span class=\"hljs-string\">'len was called...'<\/span>)\n        <span class=\"hljs-keyword\">return<\/span> self.length\n\n\n\npayroll = Payroll(<span class=\"hljs-number\">0<\/span>)\nprint(bool(payroll))  <span class=\"hljs-comment\"># False<\/span>\n\npayroll.length = <span class=\"hljs-number\">10<\/span>\nprint(bool(payroll))  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>Payroll<\/code> class doesn&#8217;t override the <code>__bool__<\/code> method, Python looks for the <code>__len__<\/code> method when evaluating the Payroll&#8217;s objects to a boolean value.<\/p>\n\n\n\n<p>In the following example payroll&#8217;s <code>__len__<\/code> returns 0, which is <code>False<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">payroll = Payroll(<span class=\"hljs-number\">0<\/span>)\nprint(bool(payroll))  <span class=\"hljs-comment\"># False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, the following example <code>__len__<\/code> returns 10 which is <code>True<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">payroll.length = <span class=\"hljs-number\">10<\/span>\nprint(bool(payroll))  <span class=\"hljs-comment\"># True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>All objects of custom classes return <code>True<\/code> by default.<\/li>\n\n\n\n<li>Implement the <code>__bool__<\/code> method to override the default. The <code>__bool__<\/code> method must return either <code>True<\/code> or <code>False<\/code>. <\/li>\n\n\n\n<li>If a class doesn&#8217;t implement the <code>__bool__<\/code> method,  Python will use the result of the <code>__len__<\/code> method. If the class doesn&#8217;t implement both methods, the objects will be <code>True<\/code> by default.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=__bool__\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2595\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__bool__\/\"\n\t\t\t\tdata-post-title=\"Python __bool__\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2595\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__bool__\/\"\n\t\t\t\tdata-post-title=\"Python __bool__\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to implement the Python __bool__ method to return boolean values for objects of a custom class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2595","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2595"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2595\/revisions"}],"predecessor-version":[{"id":7303,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2595\/revisions\/7303"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}