Boolean tests [was Re: Attack a sacred Python Cow]
pavlovevidence at gmail.com
Wed Jul 30 06:28:10 CEST 2008
On Jul 29, 7:30 pm, Steven D'Aprano <st... at REMOVE-THIS-
> On Tue, 29 Jul 2008 10:30:43 -0700, Carl Banks wrote:
> > On Jul 29, 5:15 am, Heiko Wundram <modeln... at modelnine.org> wrote:
> >> I can't dig up a simple example from code I wrote quickly, but because
> >> of the fact that explicit comparisons always hamper polymorphism
> > I'm not going to take your word for it. Do you have code that
> > demonstrates how "if x" improves polymorphism relative to simple
> > explicit tests?
> On the rapidly decreasing chance that you're not trolling (looking more
> and more unlikely every time you post):
> # The recommended way:
> if x:
> # Carl's so-called "simple explicit tests" applied to polymorphic code:
No, the following isn't my way.
> # could be a sequence or mapping?
> # WARNING: must do this test *before* the number test, otherwise
> # "if  != 0" will return True, leading to the wrong branch being
> # taken.
> if len(x) != 0:
> except AttributeError:
> # not a sequence or mapping, maybe it's a number of some sort
> except TypeError:
> # not convertable to numbers
> # FIXME: not really sure what to do here for arbitrary types
> # so fall back on converting to a boolean, and hope that works
> if bool(x):
> if x != 0:
I say that you will never, ever have to do this because there isn't a
do_something that's actually useful for all these types.
Ok, tell me, oh indignant one, what is do_something? What could
possibly be the contents of do_something such that it actually does
something useful? If you can tell me what do_something is you will
have answered my question.
More information about the Python-list