[Python-ideas] Break the dominance of boolean values in boolean context

Bruce Leban bruce at leapyear.org
Tue Sep 13 03:08:30 CEST 2011


Philosophically, the idea that boolean operations like x in y should be able
to return any value for True makes sense. The details leave much to be
desired. It wouldn't make sense for it to return x since 0 in [0,1] and ''
in 'string' should both return a true value and 0 and '' are false.


On Mon, Sep 12, 2011 at 1:20 PM, Lukas Lueg <lukas.lueg at googlemail.com>wrote:

>
> x = set((1,2,3,4,5))
> y = set((2,3,4))
> z = x > y
> print z == True
> >> True
> print z
> >> set([1,5]) # equivalent to x - y
>

You've lost me where you suggest changing the behavior of == True. And where
you propose that x > y should return x - y. You also don't mention what x <
y should return. The real problem is the lack of a compelling scenario why
you'd want to make this change.

Aside from that, checking to see if one set is contained in another does not
require computation of the full difference. Why should

very_big_set > (1,2)


go to the overhead of constructing a still very big set that's going to be
immediately discarded?


>
> z = x < y
> print z == True
> >> False
> print z
> >> set([]) # equivalent to y - x
>
> print 3 > 2
> >> 1
> print (3 > 2) == True
> >> True
>

I don't know the scenario where 3 > 2 returning 1 is useful. Would 2 < 3
also return 1? The Icon programming language (and others) has 2 < 3 return
3. This makes expressions like 2 < 3 < 4 work. And in fact python and/or
operators work in exactly the same way returning the value that determines
the answer (2 and 3) equals 3.

Icon can do this because it has a special result 'fail' which blocks further
evaluation, roughly equivalent to 1 < 0 throwing an exception. It wouldn't
work in Python for lots of reasons.


> The object-example from above now tells us how boolean behaviour and
> arithmetic behaviour go hand in hand: "(setA > setB) or (setA < setB)"
> is True because "set([1,5]).__bor__(set([]))" is the same as
> "set([1,5]) + set([])" and equivalent to True in a boolean context.
> Likewise, "(setA > setB) and (setA < setB)" is False because
> "set([1,5]).__band__set([])" is just "set([])". It follows that "(setA
> > setB) == (setA - setB) == (setA & setB)".


So what? This isn't a general principle or anything. All you've done is say
that if you define set<set to return non-bool values and __band__ and
__bor__ preserve the invariants

bool(x.__band__(y)) is bool(x and y)
bool(x.__bor__(y)) is bool(x or y)

This doesn't prove that "(setA > setB) or (setA < setB)" is always true or
anything (because it isn't of course).


> We can't do this with
> boolean operations being part of the language only.
>

Nor do we need to. Figuring out the value of "(setA > setB) or (setA <
setB)" is much more less convoluted than that.

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110912/1ad4be46/attachment.html>


More information about the Python-ideas mailing list