Strongness of Python == vs in operator [duplicate]










3
















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 inoperator, 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 theFalse` 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 returning True

  • 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. Nevertheless print(a) results False.









share|improve this question















marked as duplicate by deceze python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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 == Truewould 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





    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
















3
















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 inoperator, 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 theFalse` 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 returning True

  • 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. Nevertheless print(a) results False.









share|improve this question















marked as duplicate by deceze python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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 == Truewould 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





    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














3












3








3









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 inoperator, 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 theFalse` 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 returning True

  • 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. Nevertheless print(a) results False.









share|improve this question

















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 inoperator, 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 theFalse` 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 returning True

  • 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. Nevertheless print(a) results False.




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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 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 == Truewould 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





    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












  • @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 == Truewould 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





    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 == Truewould 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 == Truewould 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













0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto