keyword 'in' not returning a bool?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c james

    keyword 'in' not returning a bool?

    Try this
    >>sample = {'t':True, 'f':False}
    >>'t' in sample
    True
    >>type('t' in sample)
    <type 'bool'>
    >>'t' in sample == True
    False

    Why is this? Now try
    >>bool('t' in sample) == True
    True

    Can someone explain what is going on?

  • Larry Bates

    #2
    Re: keyword 'in' not returning a bool?

    c james wrote:
    Try this
    >
    >>>sample = {'t':True, 'f':False}
    >>>'t' in sample
    True
    >>>type('t' in sample)
    <type 'bool'>
    >>>'t' in sample == True
    False
    >
    Why is this? Now try
    >>>bool('t' in sample) == True
    True
    >
    Can someone explain what is going on?
    >
    Precedence. In:

    't' in sample == True

    sample == True is evaluated first
    then 't' in that which is false

    you should write

    ('t' in sample) == True

    but that is unnecessary because

    t' in sample

    is easier to read and to code and less prone to this problem.

    -Larry

    Comment

    • Richard Brodie

      #3
      Re: keyword 'in' not returning a bool?


      "c james" <cjames@callone .netwrote in message
      news:mailman.52 1.1202490602.92 67.python-list@python.org ...
      >>>'t' in sample == True
      False
      It's comparison operator chaining:

      't' in sample == True is like 't' == sample == True

      and is equivalent to 't' in sample and sample == True


      Comment

      • Arnaud Delobelle

        #4
        Re: keyword 'in' not returning a bool?

        On Feb 8, 5:09 pm, c james <cja...@callone .netwrote:
        Try this
        >
        >sample = {'t':True, 'f':False}
        >'t' in sample
        True
        >type('t' in sample)
        <type 'bool'>
        >'t' in sample == True
        >
        False
        >
        Why is this?  Now try>>bool('t' in sample) == True
        >
        True
        >
        Can someone explain what is going on?
        This is because in Python '==' and 'in' are comparison operators and
        in Python you can chain comparisons.

        Typical usage is:

        if 3 <= x < 9: # meaning (3 <= x) and (x < 9)
        # do something...

        So 't' in sample == True means the same as

        ('t' in sample) and (sample == True)

        Which will never be true as sample is a dictionary.

        To solve this, use brackets:
        >>('t' in sample) == True
        True

        HTH

        --
        Arnaud

        Comment

        • Sion Arrowsmith

          #5
          Re: keyword 'in' not returning a bool?

          In article <mailman.521.12 02490602.9267.p ython-list@python.org >,
          c james <cjames@callone .netwrote:
          >>>sample = {'t':True, 'f':False}
          >>>'t' in sample == True
          >False
          >
          >Why is this?


          "Comparison s can be chained arbitrarily; for example, x < y <= z is
          equivalent to x < y and y <= z, except that y is evaluated only once
          [ ... ]
          Two more operations with the same syntactic priority, "in" and "not
          in", are supported only by sequence types"

          So:
          't' in sample == True
          is equivalent to:
          't' in sample and sample == True
          and obviously:
          >>sample == True
          False

          --
          \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
          "Frankly I have no feelings towards penguins one way or the other"
          -- Arthur C. Clarke
          her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

          Comment

          Working...