Movatterモバイル変換


[0]ホーム

URL:


Booleans (was: Conditional operator in Python?)

Bruce Sassbsass at freenet.edmonton.ab.ca
Mon Apr 2 15:31:34 EDT 2001


On Mon, 2 Apr 2001, Russell E. Owen wrote:>  Erik Max Francis <max at alcyone.com> wrote:> >...> >On a vaguely related subject, why is there no Boolean type in Python?> >Seems like it would clean things up a great deal -- my understanding is> >that there is even an internal Python Boolean type, but it is hidden in> >the interpreter.  How come?>> I suspect the basic reason is because C doesn't have one. But that's> just a guess.I figured it was because a "boolean" is more theoretical than anythingelse, or at least never the whole story.  Tri-state would be much moreuseful -- true, false, don't know -- but what do you do in the case of"don't know"... use a default truth value, the tv of another object(determined at the time the boolean is created, or when the tv isbeing looked at?), or provide a hook and let the coder handle it(ending up with different classes of booleans)?> I personally dislike the concept that "every variable has a boolean> value", and the associated fact that utterly different values have the> same logical value. But hey, a large numer of languages work this way> and it doesn't seem to cause too much trouble. (Though I think Python> would benefit from a built in function that tests for logical equality> -- if I'm missing one, please enlighten me; the best I've come up with> so far is: not a == not b, which is not exactly obvious.)If you use 0 == false and 1 == true, the bit-wise exclusive-or is anegative logic version of what you want:A B  xor0 0   00 1   11 0   11 1   0I coded up a tri-state "Boolean" class awhile back that supportsall the logical operations, truth-value testing, and "mixed mode"boolean arithmetic (* == and, + == or)... I could clean it up and makeit public if anyone is interested (although a consensus on thequestions I posed above would be helpful, since that is where I gotwishy-washy before I moved onto something else).> But even if the Python development team suddenly became convinced that a> separate boolean type was the way to go, it'd be quite a challenge to> implement such a thing without breaking a vast amount of existing code.Why would adding something to the language break old code...just don't change the old behaviour.- Bruce


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp