Equivalent code to the bool() built-in function

Ben Finney ben+python at benfinney.id.au
Sun Apr 17 05:46:09 EDT 2011


candide <candide at free.invalid> writes:

> First because I was doubting the true interest of the bool() type. In
> fact, it appears that it's _semantically_ a shortcut for
> True if x else False.

That bends my brain. Both ‘True’ and ‘False’ are instances of the ‘bool’
type. So of course the ‘bool’ constructor will return them. What is the
“shortcut” you refer to?

Remember, the point of a type is to encapsulate both behaviour and an
exclusive domain of possible values. The Boolean type is particularly
simple, so I don't see why you would be surprised that the behaviour of
the ‘bool’ constructor is simple – that's a feature of that type.

> In the orther hand, I'm builting a sort of documentation for learning
> Python.

Something different from <URL:http://docs.python.org/tutorial/>.

> In order to spell out the purposes of the bool() type, I guessed that
> giving an equivalence code could help.

The purpose of the ‘bool’ type is to have a data type which captures the
behaviour of only Boolean values, and no other values.

-- 
 \       Eccles: “I just saw the Earth through the clouds!”  Lew: “Did |
  `\      it look round?”  Eccles: “Yes, but I don't think it saw me.” |
_o__)                            —The Goon Show, _Wings Over Dagenham_ |
Ben Finney



More information about the Python-list mailing list