Question About Logic In Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Adam

    #16
    Re: Question About Logic In Python

    Steve Holden wrote:[color=blue]
    > Ron Adam wrote:[/color]
    [color=blue][color=green]
    >>
    >> 2. Expressions that will be used in a calculation or another
    >> expression.
    >>[/color]
    > By which you appear to mean "expression s in which Boolean values are
    > used as numbers".[/color]

    Or compared to other types, which is common.

    [color=blue][color=green]
    >> This matters because if you aren't careful your results may not be
    >> what you expect.
    >>[/color]
    > Well yes, but then I wouldn't necessarily expect good results if I tried
    > to use a nail file as a tyre-lever, either. If you abuse the intent of
    > anything sufficiently you should expect trouble. But then, you seem to
    > like trouble ;-)[/color]

    Steve... You seem to agree, but think if any one has a problem with
    this, it's a misuse or an abuse. Ok. fair enough.

    Its not so much I like trouble, It's more that I tend to get stuck on
    contradictions and inconsistencies . They bug me. (sometimes I bug
    myself as well) ;-)

    [color=blue][color=green][color=darkred]
    >> >>> True * True[/color]
    >> 1 # Why not return True here as well?
    >>[/color]
    > Why not return 42? Why not return a picture of a banana?[/color]

    My question still stands. Could it be helpful if bools were preserved
    in more cases than they are now?

    My feelings is that it isn't needed as long as you strictly use your own
    modules, functions, and classes, and can depend on the behavior you
    decide on. But if you are using routines and object written by others,
    you can't always be sure if a values should be treated as a bool or as
    an integer. Preserving bools when possible, may help in that regard.

    [color=blue][color=green]
    >> This is like adding two integer types and getting a float.
    >>[/color]
    > No it isn't. It's like trying to multiply April 2 1994 by June 5 2005.
    > The operation isn't defined. So you choose an arbitrary definition and
    > say "it would be nice if it worked like this instead of how it actually
    > does work".[/color]

    Sure, but if we didn't suggest changes, then nothing would change. We
    would just have extremely detailed documentation of everything and
    strict rules of use so that we don't misuse them. ;-)

    [color=blue]
    > When in fact it doesn't really "work" at all, except for the most
    > tenuous possible value of "work". It's an accident, because Guido
    > decided that least code breakage was good when Booleans were introduced.[/color]

    Good point, but it could be changed in Python 3000 since it will drop
    the backwards compatible requirement. This doesn't mean that it should
    though. The reasons for doing so still need to be sufficient.

    [color=blue][color=green]
    >> There's the possibility of adding two (normally) True values and
    >> getting a False result.
    >>[color=darkred]
    >> >>> a = True
    >> >>> b = -1
    >> >>> a + b # True_value + True = False_value[/color]
    >> 0
    >>[/color]
    > Which is yet another reason why it makes absolutely no sense to apply
    > arithmetic operations to Boolean values.[/color]

    Again you agree, yet disagree. Ok, I see your point about breaking
    code, but should this be changed, either throw an exception or by
    changing to a boolean operation in Python 3000? You already consider it
    an abuse.

    [color=blue]
    > You are, of course, ignoring the huge amount of code breakage this
    > little change you'd find so convenient would cause.[/color]

    I'm not suggesting anything be changed before Python 3000 which has as a
    purpose of changing things that haven't been changed because of
    backwards compatibility. And I don't think I'm the only one who has
    suggested these.

    [color=blue][color=green]
    >> On one hand these seem like little things, but little things is
    >> sometimes what will bite you the hardest as they are more likely to
    >> get by your guard.
    >>[/color]
    > Kindly think again about the vast number of times that Python
    > programmers have relied on the documented property of "and", which says
    > that it returns the left operand (without evaluating the right one)
    > unless the left operand is equivalent to False, in which case it returns
    > the right operand.[/color]

    The shortcut behavior would still work, but the returned values would be
    either True or False. I agree the above is useful behavior, but I also
    see the boolean behavior as desirable. I would like both in clear and
    separate contexts if possible.

    Guido said the other day on python-dev, he would consider having 'and'
    and 'or' return True and False in order to reduce bugs, if a trinary was
    also added. It's not as short to type, but the same functionality would
    still be present in the language.

    Another possibility would be to just add bool operators '||' and '&&'
    which would always return True and False and leave 'and' and 'or' as
    they are.

    [color=blue]
    > You talk about "least surprises" and "in my opinion" as though your
    > opinions are the only ones that anyone would dream of holding. This is
    > in itself quite surprising to me.[/color]

    No, "in my opinion" means exactly that, It's my personal opinion. You
    are free and welcome to express "your opinion" as well.

    And "least surprises" is, I believe, a good thing to strive for.
    Weather or not this would fit that I'm not sure. It's quite possible
    that some of these changes would create more surprises not less.

    Also these ideas aren't mine, they are fairly standard concepts that
    other languages use as well, nothing really new here.

    Cheers,
    Ron

    Comment

    • Terry Reedy

      #17
      Re: Question About Logic In Python


      "Steve Holden" <steve@holdenwe b.com> wrote in message
      news:dgtu7d$o7d $1@sea.gmane.or g...[color=blue]
      > Which is yet another reason why it makes absolutely no sense to apply
      > arithmetic operations to Boolean values.[/color]

      Except for counting the number of true values. This and other legitimate
      uses of False/True as 0/1 (indexing, for instance) were explicitly
      considered as *features* of the current design when it was entered. The
      design was not merely based on backwards compatibility, but also on
      actually use cases which Guido did not want to disable. There was lots of
      discussion on c.l.p.

      Terry J. Reedy



      Comment

      • Terry Hancock

        #18
        Re: Question About Logic In Python

        On Thursday 22 September 2005 12:26 pm, Ron Adam wrote:[color=blue]
        > Steve Holden wrote:[color=green]
        > > Ron Adam wrote:[color=darkred]
        > >> >>> True * True
        > >> 1 # Why not return True here as well?
        > >>[/color]
        > > Why not return 42? Why not return a picture of a banana?[/color]
        >
        > My question still stands. Could it be helpful if bools were preserved
        > in more cases than they are now?[/color]

        No. "*" is "multiplication ".
        The multiplication operator is undefined for boolean values. It only
        makes sense if they are interpreted as numbers. As it happens, both
        can be coerced to 1, so the result is 1*1. This makes perfect sense
        to me.
        [color=blue][color=green][color=darkred]
        >>> True and True[/color][/color][/color]
        True

        Also makes sense (and this is indeed what happens).

        Cheers,
        Terry

        --
        Terry Hancock ( hancock at anansispacework s.com )
        Anansi Spaceworks http://www.anansispaceworks.com

        Comment

        • Bengt Richter

          #19
          Re: Question About Logic In Python

          On Thu, 22 Sep 2005 14:12:52 -0400, "Terry Reedy" <tjreedy@udel.e du> wrote:
          [color=blue]
          >
          >"Steve Holden" <steve@holdenwe b.com> wrote in message
          >news:dgtu7d$o7 d$1@sea.gmane.o rg...[color=green]
          >> Which is yet another reason why it makes absolutely no sense to apply
          >> arithmetic operations to Boolean values.[/color]
          >
          >Except for counting the number of true values. This and other legitimate
          >uses of False/True as 0/1 (indexing, for instance) were explicitly
          >considered as *features* of the current design when it was entered. The
          >design was not merely based on backwards compatibility, but also on
          >actually use cases which Guido did not want to disable. There was lots of
          >discussion on c.l.p.
          >[/color]
          OTOH ISTM choosing to define bool as a subclass of int was a case of
          practicality beats purity, but I'm not sure it wasn't an overeager coercion in disguise.

          I.e., IMO a boolean is really not an integer, but it does belong to an ordered set,
          or enumeration, that has a very useful correspondence to the enumeration of binary digits,
          which in turn corresponds to the beginning subset of the ordered set of the non-negative integers.
          IIRC Pascal actually does use an enumeration to define its bools, and you get the integer values
          via an ord function.

          BTW, for counting you could always use sum(1 for x in boolables if x) ;-)

          And giving the bool type an __int__ method of its own might have covered a lot of coercions
          of convenience, especially if __getitem__ of list and tuple would do coercion (you
          could argue about coercing floats, etc. in that context, as was probably done ;-)

          Regards,
          Bengt Richter

          Comment

          • Ron Adam

            #20
            Re: Question About Logic In Python

            Terry Hancock wrote:[color=blue]
            > On Thursday 22 September 2005 12:26 pm, Ron Adam wrote:
            >[color=green]
            >>Steve Holden wrote:
            >>[color=darkred]
            >>>Ron Adam wrote:
            >>>
            >>>> >>> True * True
            >>>>1 # Why not return True here as well?
            >>>>
            >>>
            >>>Why not return 42? Why not return a picture of a banana?[/color]
            >>
            >>My question still stands. Could it be helpful if bools were preserved
            >>in more cases than they are now?[/color]
            >
            >
            > No. "*" is "multiplication ".
            > The multiplication operator is undefined for boolean values. It only
            > makes sense if they are interpreted as numbers. As it happens, both
            > can be coerced to 1, so the result is 1*1. This makes perfect sense
            > to me.[/color]

            I'm not implying it doesn't make sense. It does to me as well. And it
            would only make sense to return bools in this case if Python supported
            boolean math.

            If it did, coercion to ints (or floats) would still occur with mixed
            types are used in expressions, but there are some situations where the
            bool-bool results differ, so it's most likely an all or nothing move.

            Both views are valid and there are benefits to both as well.

            [color=blue][color=green][color=darkred]
            >>>>True and True[/color][/color]
            >
            > True
            >
            > Also makes sense (and this is indeed what happens).[/color]

            Only because True is the last value here. ;-)


            [color=blue]
            > Cheers,
            > Terry[/color]


            Cheers,
            Ron













            Comment

            • Steve Holden

              #21
              Re: Question About Logic In Python

              Terry Reedy wrote:[color=blue]
              > "Steve Holden" <steve@holdenwe b.com> wrote in message
              > news:dgtu7d$o7d $1@sea.gmane.or g...
              >[color=green]
              >>Which is yet another reason why it makes absolutely no sense to apply
              >>arithmetic operations to Boolean values.[/color]
              >
              >
              > Except for counting the number of true values. This and other legitimate
              > uses of False/True as 0/1 (indexing, for instance) were explicitly
              > considered as *features* of the current design when it was entered. The
              > design was not merely based on backwards compatibility, but also on
              > actually use cases which Guido did not want to disable. There was lots of
              > discussion on c.l.p.
              >[/color]
              Sure. Perhaps I should have said it makes absolutely no sense "to expect
              the results of aritmetic operations on bools to themselves be bools".
              Sheesh, you have to be so careful nowadays ... :-)

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC www.holdenweb.com
              PyCon TX 2006 www.pycon.org

              Comment

              • Terry Hancock

                #22
                Re: Question About Logic In Python

                On Thursday 22 September 2005 07:09 pm, Ron Adam wrote:[color=blue]
                > Terry Hancock wrote:[color=green]
                > > On Thursday 22 September 2005 12:26 pm, Ron Adam wrote:[color=darkred]
                > >>>>True and True[/color]
                > >
                > > True
                > >
                > > Also makes sense (and this is indeed what happens).[/color]
                >
                > Only because True is the last value here. ;-)[/color]

                Nope, works for False, too:
                [color=blue][color=green][color=darkred]
                >>> True and False[/color][/color][/color]
                False

                I see what you mean, but if you were mixing types, then
                you probably wanted the "value preserved" behavior. If
                both objects are bool, the result is too (even if this
                is only a coincidence, it still happens to be true).

                So this still makes sense:
                [color=blue][color=green][color=darkred]
                >>> True and 1[/color][/color][/color]
                1

                Effectively, what you'd be asking for is to have bool
                coerce more strongly than ints or other types, so that
                results would become boolean if any boolean argument
                existed. But that would be a pretty major change in Python,
                and break lots of code.



                --
                Terry Hancock ( hancock at anansispacework s.com )
                Anansi Spaceworks http://www.anansispaceworks.com

                Comment

                • Bryan Olson

                  #23
                  Re: Question About Logic In Python

                  Steven D'Aprano wrote:
                  [color=blue]
                  > Or wait, I have thought of one usage case: if you are returning a value
                  > that you know will be used only as a flag, you should convert it into a
                  > bool. Are there any other uses for bool()?[/color]

                  We could, of course, get along without it. One use for
                  canonical true and false values is to give 'not' something
                  sensible to return.


                  --
                  --Bryan

                  Comment

                  Working...