Weird expression result

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Sakkis

    Weird expression result

    I'm probably missing something obvious but I can't put my finger on
    it:
    >>(3 in [3]) == True
    True
    >>3 in ([3] == True)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: argument of type 'bool' is not iterable
    >>3 in [3] == True
    False

    How/why does the last one evaluate to False ?

    George
  • Peter Otten

    #2
    Re: Weird expression result

    George Sakkis wrote:
    I'm probably missing something obvious but I can't put my finger on
    it:
    >
    >>>(3 in [3]) == True
    True
    >
    >>>3 in ([3] == True)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: argument of type 'bool' is not iterable
    >
    >>>3 in [3] == True
    False
    >
    How/why does the last one evaluate to False ?
    >
    George
    This works just like a < b < c:
    >>3 in [3] and [3] == True
    False

    Peter

    Comment

    • Dan Lenski

      #3
      Re: Weird expression result

      On Mon, 18 Aug 2008 18:04:32 +0200, Peter Otten wrote:
      This works just like a < b < c:
      >
      >>>3 in [3] and [3] == True
      False
      >
      Peter
      Interesting. I agree with the OP that it is confusing!

      Does this expansion really get invoked every time there is an expression
      of the form??

      expr1 binary_op1 expr2 binary_op2 expr3 ...

      Seemingly, the answer is yes:
      >>3 in [3] in [[3],[4]] in [[[3],[4]],5] == [[[3],[2+2]],5]
      True

      How does this play with standard precedence rules?

      Dan

      Comment

      • cokofreedom@gmail.com

        #4
        Re: Weird expression result

        On Aug 18, 5:57 pm, George Sakkis <george.sak...@ gmail.comwrote:
        I'm probably missing something obvious but I can't put my finger on
        it:
        >
        >(3 in [3]) == True
        >
        True
        >
        >3 in ([3] == True)
        >
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        TypeError: argument of type 'bool' is not iterable
        >
        >3 in [3] == True
        >
        False
        >
        How/why does the last one evaluate to False ?
        >
        George
        >(3 in [3]) == True
        The list holds an integer value of 3, you check if 3 is in that list,
        that is true, you then check it your answer is true...
        >3 in ([3] == True)
        True and False are keywords now and do not correspond the way you
        think. [3] == True or [3] == False will both answer False.

        Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
        (Intel)] on win32
        Type "help", "copyright" , "credits" or "license" for more information.
        >>a = list()
        >>a.append(3)
        >>3 in a
        True
        >>if a:
        .... print "yay"
        ....
        yay
        >>if a == True:
        .... print "yay"
        .... else:
        .... print "nay"
        ....
        nay
        >3 in [3] == True
        http://docs.python.org/ref[3/summary.html
        >>3 in [3] == True
        False
        >>3 in [3] == False
        False

        However I come a bit stuck myself, since this should just tell you
        that you cannot do a membership test on 3 being in a bool...but it
        doesn't...It should be doing the [3] == True, returning false then
        trying to see if 3 is in false...

        Comment

        • Peter Otten

          #5
          Re: Weird expression result

          Dan Lenski wrote:
          On Mon, 18 Aug 2008 18:04:32 +0200, Peter Otten wrote:
          >This works just like a < b < c:
          >>
          >>>>3 in [3] and [3] == True
          >False
          Interesting. I agree with the OP that it is confusing!
          >
          Does this expansion really get invoked every time there is an expression
          of the form??
          >
          expr1 binary_op1 expr2 binary_op2 expr3 ...
          >
          Seemingly, the answer is yes:
          >
          >>3 in [3] in [[3],[4]] in [[[3],[4]],5] == [[[3],[2+2]],5]
          True
          >
          How does this play with standard precedence rules?
          Simple, all comparisons have the same priority:



          Peter

          Comment

          • George Sakkis

            #6
            Re: Weird expression result

            On Aug 18, 12:04 pm, Peter Otten <__pete...@web. dewrote:
            George Sakkis wrote:
            I'm probably missing something obvious but I can't put my finger on
            it:
            >
            >>(3 in [3]) == True
            True
            >
            >>3 in ([3] == True)
            Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
            TypeError: argument of type 'bool' is not iterable
            >
            >>3 in [3] == True
            False
            >
            How/why does the last one evaluate to False ?
            >
            George
            >
            This works just like a < b < c:
            >
            >3 in [3] and [3] == True
            >
            False
            >
            Peter

            Argh, you're right! I think chaining makes sense only for comparison
            operators, for anything else it's not obvious. That came up from a
            real bug by the way, it's not made up.

            George

            Comment

            • Fredrik Lundh

              #7
              Re: Weird expression result

              cokofreedom@gma il.com wrote:
              >>>>3 in [3] == True
              >
              http://docs.python.org/ref[3/summary.html
              that page is broken, as recently mentioned; "in", "not in", "is", and
              "is not" are comparison operators too, chains in the same way as the
              others. for details, see:



              </F>

              Comment

              • Dan Lenski

                #8
                Re: Weird expression result

                On Mon, 18 Aug 2008 18:27:53 +0200, Peter Otten wrote:
                Dan Lenski wrote:
                >How does this play with standard precedence rules?
                >
                Simple, all comparisons have the same priority:
                >

                >
                Peter
                I see. So, since the comparison operators have lower precedence than
                everything else... it won't affect any terms involving non-comparison
                operators.

                Dan

                Comment

                Working...