Python Forum
str in str == True - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: str in str == True (/thread-17045.html)



str in str == True - speedskis777 - Mar-26-2019

Why do my 3rd and 4th lines give different results? 3 prints True, 4 prints False.

Thanks!

blue = "lm"
my_size = "l"
print(my_size in blue)
print(my_size in blue == True)



RE: str in str == True - buran - Mar-26-2019

line 4 is equal to
print(my_size in (blue == True))
which is
print(my_size in False)


EDIT:
Obviously I was wrong - see at @micseydel's post below


RE: str in str == True - micseydel - Mar-26-2019

buran, I love you buddy but that's not true.
>>> print('l' in 'lb' == True)
False
>>> print('l' in ('lb' == True))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>> print(('l' in 'lb') == True)
True
I'm actually baffled as to how this is returning False. I plan to look into it a little bit more, expect a followup...


RE: str in str == True - micseydel - Mar-27-2019

Haven't had a chance yet to review this but my very smart roommate said something I imagine is helpful:
Mikel Wrote:According to dis.dis(lambda: 'a' in 'ab' == True), ('a' in 'ab' == True) is equivalent to ('a' in 'ab' and 'ab' == True)
I think I know why: comparisons in Python are allowed to written similar to what we do naturally
So x < y < z is valid and equivalent to x < y and y < z
The 'in' operator is treated equivalently to '<', '==', etc.
So x in y == z is equivalent to x in y and y == z
Mystery solved



RE: str in str == True - micseydel - Mar-27-2019

Still need to read that but in the mean time I wanted suggest a solution - just use the first form of the two. The second form gains you nothing, it's generally seen as newby in Python to compare to a boolean (since any expression can be treated as a boolean in most important situations, the shorter code doesn't have any disadvantage).


RE: str in str == True - micseydel - Mar-27-2019

Ok yeah it makes sense now. Bummer that that Python feature has such a downside, since I'm surprised by the result of the expression you provided and definitely wouldn't have expected it. Trade-offs, I guess.


RE: str in str == True - buran - Mar-27-2019

(Mar-26-2019, 11:25 PM)micseydel Wrote: buran, I love you buddy but that's not true.
I admit this one of the rare times when I post without actually run the code I post Blush Angry


RE: str in str == True - snippsat - Mar-27-2019

Yes Mikel is right,can look a little at grouping of this.
So i looked a little into comparison operator chaining.
So my understand of this is.
>>> blue = "lm"
>>> my_size = "l"
>>> my_size in blue
True
>>> my_size in blue == True
False

>>> # Chaining (this is what comparison operators do) left to right
>>> (my_size in blue) and (blue == True)
False
>>> # So it is 
>>> True and False
False
If look at grouping for non-comparison operators.
>>> 7 - 1 + 4
10 
>>> # Means
>>> (7 - 1) + 4
10
>>> # Grouping right to left would give a different result
>>> 7 - (1 + 4)
2 
One more with comparison operator chaining
a < b < c 
# Grouping is
(a < b) and (b < c)
The other way around:
>>> blue = "lm"
>>> my_size = "a"
>>> my_size in blue
False
>>> # Both are False 
>>> my_size in blue == True
False
>>> my_size in blue == False
False

>>> # Comparison operator chaining
>>> (my_size in blue) and (blue == True)
False
>>> # So if first grouping is False,then always return False
>>> (my_size in blue)
False
>>> # Then second group is not looked at all,as first group is False
>>> (blue == True)
False

# So we never get to this
>>> False == False
True