weird behaviour of "0 in [] is False"

D

Diez B. Roggisch

l = []
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iterable argument required

that should be clear - 0 in False can't work.

It seems to stem from a behaviour python exhibits in expressions like this:

3 > 2 > 1

This yields True, while

(3 > 2 ) > 1

yields false. Chaining of operators gets translated like this:

3 > 2 and 2 > 1

Look in section 5.9 of the language reference.

Then your expression gets translated to:

0 in l and l is False

which yields False of course.
 
D

Duncan Booth

Sylvain said:
Hi there !

Can someone explain me the following behaviour ?
l = []
0 in (l is False)
Traceback (most recent call last):
False


This is really obscur to me...

From the language reference (5.9 Comparisons):
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"

...snip...

Formally, if a, b, c, ..., y, z are expressions and opa, opb, ..., opy
are comparison operators, then a opa b opb c ...y opy z is equivalent
to a opa b and b opb c and ... y opy z, except that each expression is
evaluated at most once.

In other words '0 in l is False' is equivalent to '0 in l and l is False'.
 
S

Sylvain Thenault

l = []
0 in (l is False)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iterable argument required

that should be clear - 0 in False can't work.

yes, I forget to mention that it was the third expression which was
bugging me.
It seems to stem from a behaviour python exhibits in expressions like
this:

3 > 2 > 1

This yields True, while

(3 > 2 ) > 1

yields false. Chaining of operators gets translated like this:

3 > 2 and 2 > 1

Look in section 5.9 of the language reference.

Then your expression gets translated to:

0 in l and l is False

which yields False of course.

thanks, I had missed this part of the language reference !
Not yet found an misbehaviour in python... ;)
 
P

Paul Robson

Sylvain said:

(l is False) is not a tuple or list, it's a boolean value.
Traceback (most recent call last):

True

0 in l is False becuase l is empty, so it's False is False which is true,
(except in Intercal probably and Visual C++)

l is False is False because l is not the value false though it has a false
value (err.....)

Okay.

l != False because it's not the displayed value false

but if not l would evaluated to true because [] is a false equivalent.

0 in False .... okay.... this should be an error ..... something to do with
the equivalence confusion of what False is ?
 
S

Scott David Daniels

Sylvain said:
Hi there !

Can someone explain me the following behaviour ?

l = []
0 in (l is False)

Traceback (most recent call last):

False


This is really obscur to me...
A suggestion:
When discussing things on the newsgroup (and in your code), avoid
symbols like "l" and "O" -- they look too much like numbers. Picking
an actual name is best (but depends on your application context).
Second best are names like: "lst", "obj", ...: often, however, such
names are necessary when discussing abstract code properties.

-Scott
 
J

John Roth

Paul Robson said:
Sylvain said:
l = []
0 in (l is False)

(l is False) is not a tuple or list, it's a boolean value.
Traceback (most recent call last):

True

0 in l is False becuase l is empty, so it's False is False which is true,
(except in Intercal probably and Visual C++)

l is False is False because l is not the value false though it has a false
value (err.....)

Okay.

l != False because it's not the displayed value false

but if not l would evaluated to true because [] is a false equivalent.

0 in False .... okay.... this should be an error ..... something to do
with
the equivalence confusion of what False is ?

It's not an error. As one of the first responders said, check
the language definition. That defines both 'in' and 'is'
as equality operators, and defines exactly what a chain
of equality operators means.

In this case, it means:

(0 in l) and (l is False)

The and short circuits, giving the result of False without
ever doing the final comparison.

Granted, that's not exactly obvious...

John Roth
 
P

Paul Robson

John said:
It's not an error. As one of the first responders said, check
the language definition. That defines both 'in' and 'is'
as equality operators, and defines exactly what a chain
of equality operators means.

In this case, it means:

(0 in l) and (l is False)

The and short circuits, giving the result of False without
ever doing the final comparison.

Granted, that's not exactly obvious...

Thanks ; you learn something every day :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top