[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

keyword 'in' not returning a bool?

c james

2/8/2008 5:10:00 PM

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?

4 Answers

Larry Bates

2/8/2008 5:16:00 PM

0

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

Richard Brodie

2/8/2008 5:22:00 PM

0


"c james" <cjames@callone.net> wrote in message
news:mailman.521.1202490602.9267.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


Arnaud Delobelle

2/8/2008 5:34:00 PM

0

On Feb 8, 5:09 pm, c james <cja...@callone.net> 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?

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

Sion Arrowsmith

2/8/2008 6:03:00 PM

0

In article <mailman.521.1202490602.9267.python-list@python.org>,
c james <cjames@callone.net> wrote:
>>>> sample = {'t':True, 'f':False}
>>>> 't' in sample == True
>False
>
>Why is this?

http://docs.python.org/lib/compar...

"Comparisons 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.greenend.org.uk -- http://www.chaos.org...
"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