News for hackers. Stuff that matters.
Re:Python Idioms [pdf] by Redoubts on Thursday January 30, 2014 @12:10PM (#7151759)
I don't understand the truth table on slide 9 for " - vs None " and "__nonzero__ (2.x) " __bool__ (3.x) vs __nonzero__ (2.x) " __bool__ (3.x) "
Re:Python Idioms [pdf] by Arnor on Thursday January 30, 2014 @12:39PM (#7151967)
I'm pretty sure the "-" vs "None" meant that there wasn't a truthy alternative to None. More accurate would have been "N/A" vs "None". Not clear on the __nonzero__/__bool__ stuff...
Re:Python Idioms [pdf] by falcolas on Thursday January 30, 2014 @01:18PM (#7152319)
__nonzero__/__bool__ are the methods called on objects when evaluating truthiness. So, if you're implementing custom objects, those are what you would use, and return either True or False, based on their truthiness value.

As an example, you might represent __bool__ on a database connection object to reflect whether there is a live connection, allowing you to:

    if not conn:
        conn = MySQLdb.Connect()
Re:Python Idioms [pdf] by mattbot5000 on Thursday January 30, 2014 @12:44PM (#7152007)
He's referring to the special methods available to classes that allow implementation of the truth value of an object. In version 2.x of Python, this method is __nonzero__[1] and in version 3.x it's called __bool__[2]

[1] http://docs.python.org/2/reference/datamodel.html#object.__n...

[2] http://docs.python.org/3/reference/datamodel.html#object.__b...