[Chicago] falsy objects?

Jonathan Hayward christos.jonathan.hayward at gmail.com
Fri Mar 19 15:54:51 CET 2010


There was something I thought I'd read but couldn't track down in the
documentation.

User-defined classes normally evaluate to true, i.e. if you define:

class foo:
    pass

bar = foo()

if bar:
    print "True"
else:
    print "False"

then the output will be "True", and adding real functionality to foo does
not change this.

I thought there was supposed to be a method you could define that would
override this behavior, named something like is_true() or __is_true__(), so
that an object could be set to evaluate to false. However, looking through
the documentation did not confirm anything like:

class foo:
    def is_true(self):
        return False

bar = foo()

if bar:
    print "True"
else:
    print "False"

which would print "False".

Is there such a method that can be defined, or is it non-negotiable that a
user-defined class (which does not extend a class that can be falsy) will
evaluate to true?

-- 
→ Jonathan Hayward, a Senior Web Developer who cares deeply about usability
→ www.linkedin.com/in/jonathanhayward • jonathan.hayward at pobox.com
→ Ajax, CGI, CMS, CSS, HTML, IA, JSON, JavaScript, LAMP, Linux, Perl, PHP,
Python, SQL, UI, Unix, Usability, UX, XHTML, XML
→ With a good interest in the human side of computing and making software
and websites a joy to use
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20100319/441bca6e/attachment.html>


More information about the Chicago mailing list