Equivalent code to the bool() built-in function
candide
candide at free.invalid
Sun Apr 17 19:22:59 EDT 2011
Le 17/04/2011 11:46, Ben Finney a écrit :
> 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.
A user of the Python programming language can sensibly handle boolean
values and write a lot of good code and at the same time completely
ignore all the object oriented stuff behind the True and False instances.
> What is the
> “shortcut” you refer to?
bool(x) is nothing more than a shortcut for the following expression :
True if x else False.
Compare for instance with the str() type whose implementation is far
more complicated. When learning Python, you early and communly use str()
type but use sparingly the bool() type. On the other hand, the learner
cannot replace himself the service str() provide (hard to write an
equivalent code). It's not the case for the bool() type : True if foo
else False.
>
> 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 fact, bool() is so simple that at first glance I didn't see any use
for it.
>> In the orther hand, I'm builting a sort of documentation for learning
>> Python.
>
> Something different from<URL:http://docs.python.org/tutorial/>.
>
More : something _opposite_ to the official Python tutorial ;) I found
this tutorial very unfriendly and very unsmooth, poorly structured,
providing overloaded examples, unable to distinguish essential from
incidental, full of bad explanations, etc. OOP presentation is terribly
verbose and speculative, chapter on error handling is very hard to
follow if you are not aware of the exception mechanism, etc
This is not a surprise, as the abstract explains :
"This tutorial does not attempt to be comprehensive and cover every
single feature, or even every commonly used feature."
The nice graphical aspect of the tutorial (thanks to Sphinx) doesn't
compensate the many weaknesses of the content.
More information about the Python-list
mailing list