[Python-ideas] Programming recommendations (PEP 8) and boolean values

Oleg Broytman phd at phdru.name
Thu Aug 9 11:36:48 CEST 2012


On Thu, Aug 09, 2012 at 01:15:38PM +1000, Ben Finney <ben+python at benfinney.id.au> wrote:
> Oleg Broytman <phd at phdru.name> writes:
> > On Thu, Aug 09, 2012 at 02:18:53AM +1000, Ben Finney <ben+python at benfinney.id.au> wrote:
> > > What is a compelling use case for checking precisely for True or False?
> >
> > To distinguish False and None for a tri-state variable that can
> > have 3 values - "yes", "no" and "unspecified" - True, False and None
> > in Python-speak.
>> Since True and False are strongly coupled with a *two*-state type, that
> just seems needlessly confusing to the reader of the code. Better to use
> a non-bool type which makes it explicit that there are three valid
> values.

 I am not always free to use my own classes and values. Sometimes
(quite often, actually) I receive values from 3rd-party libraries.
 Example - DB API drivers:
 cursor.execute("SELECT boolean FROM mytable")
 row = cursor.fetchone()
 Now row[0] is that tri-state variable with possible values 1/0/None
or True/False/None. None is of course for SQL NULL.
Oleg.
-- 
 Oleg Broytman http://phdru.name/ phd at phdru.name
 Programmers don't die, they just GOSUB without RETURN.


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /