[Chicago] falsy objects?

Daniel Griffin dgriff1 at gmail.com
Fri Mar 19 16:01:12 CET 2010


I think you are looking for __nonzero__ and __bool__ and the evaluation of
them is different between versions of python. A quick google search doesnt
show anything promising but those are the interfaces I think.


On Fri, Mar 19, 2010 at 9:54 AM, Jonathan Hayward <
christos.jonathan.hayward at gmail.com> wrote:

> 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
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20100319/78825e48/attachment.html>


More information about the Chicago mailing list