Strongness of Python == vs in operator [duplicate]
This question already has an answer here:
Why does `True == False is False` evaluate to False? [duplicate]
4 answers
Python comparison operators chaining/grouping left to right?
1 answer
Python chained comparison
4 answers
I'm stumbling a bit why the following code
str1 = "text"
str2 = "some longer text"
if str1 in str2 == True:
pass # do something
is not working. (I know that I can easily fix this problem by writing if str1 in str2 == True:
- this is not the question!)
The question is, why is
str1 in str2 == True
returning False
? I mean if the operator in
is stronger than the ==
operator, I would have the following code
if (str1 in str2) == True:
which is actually working (as it will be evaluated to True) ... And if it is the other way round, that the ==
operator is stronger than the in
operator, I would expect an TypeError: argument of type 'bool' is not iterable
Exception. Actually the code
if a = "" in ("" == True):
is raising such an exception. So what is Python doing here? Which operator (in
vs ==
) is evaluated first? And why is the result False
:
a = ("" in "" == True)
and not True
or an TypeError: argument of type 'bool' is not iterableException? The only way I could imagine the
False` result is that Python is trying to execute both operators all together. But how is it working then?
Things I noticed:
- Python2 and Python3 are both showing the same result.
- The statement
"" in "" == ""
is actually returningTrue
- If you have the code
a = ("" in "" == True)
,type(a)
will result in<type 'bool'>
. So the result is actually a boolean - as expected - and not some kind of other object. Neverthelessprint(a)
resultsFalse
.
python python-3.x python-2.7
marked as duplicate by deceze♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 8:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does `True == False is False` evaluate to False? [duplicate]
4 answers
Python comparison operators chaining/grouping left to right?
1 answer
Python chained comparison
4 answers
I'm stumbling a bit why the following code
str1 = "text"
str2 = "some longer text"
if str1 in str2 == True:
pass # do something
is not working. (I know that I can easily fix this problem by writing if str1 in str2 == True:
- this is not the question!)
The question is, why is
str1 in str2 == True
returning False
? I mean if the operator in
is stronger than the ==
operator, I would have the following code
if (str1 in str2) == True:
which is actually working (as it will be evaluated to True) ... And if it is the other way round, that the ==
operator is stronger than the in
operator, I would expect an TypeError: argument of type 'bool' is not iterable
Exception. Actually the code
if a = "" in ("" == True):
is raising such an exception. So what is Python doing here? Which operator (in
vs ==
) is evaluated first? And why is the result False
:
a = ("" in "" == True)
and not True
or an TypeError: argument of type 'bool' is not iterableException? The only way I could imagine the
False` result is that Python is trying to execute both operators all together. But how is it working then?
Things I noticed:
- Python2 and Python3 are both showing the same result.
- The statement
"" in "" == ""
is actually returningTrue
- If you have the code
a = ("" in "" == True)
,type(a)
will result in<type 'bool'>
. So the result is actually a boolean - as expected - and not some kind of other object. Neverthelessprint(a)
resultsFalse
.
python python-3.x python-2.7
marked as duplicate by deceze♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 8:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
There is absolutely no reason to ever write== True
(*except for very specific circumstances). Just doif str1 in str2:
. What you're creating there is a chained comparison.
– deceze♦
Nov 13 '18 at 8:10
@deceze I know that there is no reason to ever write== True
. I just wanted to know what is happening when you do it nevertheless. And neither stackoverflow.com/questions/25753474/… nor stackoverflow.com/questions/24018315/python-chained-comparison is a duplicate. As explained in my question - when these operators would be chained, then the codestr1 in str2 == True
would either equal tostr1 in (str2 == True)
or(str1 in str2) == True
however this is not the case.
– quant
Nov 13 '18 at 8:16
4
Err, no, then you don't quite understand what a chain is yet. See stackoverflow.com/a/25753528/476 again. What you have is equivalent to(str1 in str2) and (str2 == True)
!
– deceze♦
Nov 13 '18 at 8:19
add a comment |
This question already has an answer here:
Why does `True == False is False` evaluate to False? [duplicate]
4 answers
Python comparison operators chaining/grouping left to right?
1 answer
Python chained comparison
4 answers
I'm stumbling a bit why the following code
str1 = "text"
str2 = "some longer text"
if str1 in str2 == True:
pass # do something
is not working. (I know that I can easily fix this problem by writing if str1 in str2 == True:
- this is not the question!)
The question is, why is
str1 in str2 == True
returning False
? I mean if the operator in
is stronger than the ==
operator, I would have the following code
if (str1 in str2) == True:
which is actually working (as it will be evaluated to True) ... And if it is the other way round, that the ==
operator is stronger than the in
operator, I would expect an TypeError: argument of type 'bool' is not iterable
Exception. Actually the code
if a = "" in ("" == True):
is raising such an exception. So what is Python doing here? Which operator (in
vs ==
) is evaluated first? And why is the result False
:
a = ("" in "" == True)
and not True
or an TypeError: argument of type 'bool' is not iterableException? The only way I could imagine the
False` result is that Python is trying to execute both operators all together. But how is it working then?
Things I noticed:
- Python2 and Python3 are both showing the same result.
- The statement
"" in "" == ""
is actually returningTrue
- If you have the code
a = ("" in "" == True)
,type(a)
will result in<type 'bool'>
. So the result is actually a boolean - as expected - and not some kind of other object. Neverthelessprint(a)
resultsFalse
.
python python-3.x python-2.7
This question already has an answer here:
Why does `True == False is False` evaluate to False? [duplicate]
4 answers
Python comparison operators chaining/grouping left to right?
1 answer
Python chained comparison
4 answers
I'm stumbling a bit why the following code
str1 = "text"
str2 = "some longer text"
if str1 in str2 == True:
pass # do something
is not working. (I know that I can easily fix this problem by writing if str1 in str2 == True:
- this is not the question!)
The question is, why is
str1 in str2 == True
returning False
? I mean if the operator in
is stronger than the ==
operator, I would have the following code
if (str1 in str2) == True:
which is actually working (as it will be evaluated to True) ... And if it is the other way round, that the ==
operator is stronger than the in
operator, I would expect an TypeError: argument of type 'bool' is not iterable
Exception. Actually the code
if a = "" in ("" == True):
is raising such an exception. So what is Python doing here? Which operator (in
vs ==
) is evaluated first? And why is the result False
:
a = ("" in "" == True)
and not True
or an TypeError: argument of type 'bool' is not iterableException? The only way I could imagine the
False` result is that Python is trying to execute both operators all together. But how is it working then?
Things I noticed:
- Python2 and Python3 are both showing the same result.
- The statement
"" in "" == ""
is actually returningTrue
- If you have the code
a = ("" in "" == True)
,type(a)
will result in<type 'bool'>
. So the result is actually a boolean - as expected - and not some kind of other object. Neverthelessprint(a)
resultsFalse
.
This question already has an answer here:
Why does `True == False is False` evaluate to False? [duplicate]
4 answers
Python comparison operators chaining/grouping left to right?
1 answer
Python chained comparison
4 answers
python python-3.x python-2.7
python python-3.x python-2.7
edited Nov 13 '18 at 8:23
quant
asked Nov 13 '18 at 8:06
quantquant
1,58211526
1,58211526
marked as duplicate by deceze♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 8:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by deceze♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 13 '18 at 8:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
There is absolutely no reason to ever write== True
(*except for very specific circumstances). Just doif str1 in str2:
. What you're creating there is a chained comparison.
– deceze♦
Nov 13 '18 at 8:10
@deceze I know that there is no reason to ever write== True
. I just wanted to know what is happening when you do it nevertheless. And neither stackoverflow.com/questions/25753474/… nor stackoverflow.com/questions/24018315/python-chained-comparison is a duplicate. As explained in my question - when these operators would be chained, then the codestr1 in str2 == True
would either equal tostr1 in (str2 == True)
or(str1 in str2) == True
however this is not the case.
– quant
Nov 13 '18 at 8:16
4
Err, no, then you don't quite understand what a chain is yet. See stackoverflow.com/a/25753528/476 again. What you have is equivalent to(str1 in str2) and (str2 == True)
!
– deceze♦
Nov 13 '18 at 8:19
add a comment |
There is absolutely no reason to ever write== True
(*except for very specific circumstances). Just doif str1 in str2:
. What you're creating there is a chained comparison.
– deceze♦
Nov 13 '18 at 8:10
@deceze I know that there is no reason to ever write== True
. I just wanted to know what is happening when you do it nevertheless. And neither stackoverflow.com/questions/25753474/… nor stackoverflow.com/questions/24018315/python-chained-comparison is a duplicate. As explained in my question - when these operators would be chained, then the codestr1 in str2 == True
would either equal tostr1 in (str2 == True)
or(str1 in str2) == True
however this is not the case.
– quant
Nov 13 '18 at 8:16
4
Err, no, then you don't quite understand what a chain is yet. See stackoverflow.com/a/25753528/476 again. What you have is equivalent to(str1 in str2) and (str2 == True)
!
– deceze♦
Nov 13 '18 at 8:19
There is absolutely no reason to ever write
== True
(*except for very specific circumstances). Just do if str1 in str2:
. What you're creating there is a chained comparison.– deceze♦
Nov 13 '18 at 8:10
There is absolutely no reason to ever write
== True
(*except for very specific circumstances). Just do if str1 in str2:
. What you're creating there is a chained comparison.– deceze♦
Nov 13 '18 at 8:10
@deceze I know that there is no reason to ever write
== True
. I just wanted to know what is happening when you do it nevertheless. And neither stackoverflow.com/questions/25753474/… nor stackoverflow.com/questions/24018315/python-chained-comparison is a duplicate. As explained in my question - when these operators would be chained, then the code str1 in str2 == True
would either equal to str1 in (str2 == True)
or (str1 in str2) == True
however this is not the case.– quant
Nov 13 '18 at 8:16
@deceze I know that there is no reason to ever write
== True
. I just wanted to know what is happening when you do it nevertheless. And neither stackoverflow.com/questions/25753474/… nor stackoverflow.com/questions/24018315/python-chained-comparison is a duplicate. As explained in my question - when these operators would be chained, then the code str1 in str2 == True
would either equal to str1 in (str2 == True)
or (str1 in str2) == True
however this is not the case.– quant
Nov 13 '18 at 8:16
4
4
Err, no, then you don't quite understand what a chain is yet. See stackoverflow.com/a/25753528/476 again. What you have is equivalent to
(str1 in str2) and (str2 == True)
!– deceze♦
Nov 13 '18 at 8:19
Err, no, then you don't quite understand what a chain is yet. See stackoverflow.com/a/25753528/476 again. What you have is equivalent to
(str1 in str2) and (str2 == True)
!– deceze♦
Nov 13 '18 at 8:19
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is absolutely no reason to ever write
== True
(*except for very specific circumstances). Just doif str1 in str2:
. What you're creating there is a chained comparison.– deceze♦
Nov 13 '18 at 8:10
@deceze I know that there is no reason to ever write
== True
. I just wanted to know what is happening when you do it nevertheless. And neither stackoverflow.com/questions/25753474/… nor stackoverflow.com/questions/24018315/python-chained-comparison is a duplicate. As explained in my question - when these operators would be chained, then the codestr1 in str2 == True
would either equal tostr1 in (str2 == True)
or(str1 in str2) == True
however this is not the case.– quant
Nov 13 '18 at 8:16
4
Err, no, then you don't quite understand what a chain is yet. See stackoverflow.com/a/25753528/476 again. What you have is equivalent to
(str1 in str2) and (str2 == True)
!– deceze♦
Nov 13 '18 at 8:19