Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
boolean conditions
#11
ichabod801 Wrote:In that case I want to distinguish between go never having been changed after a successful move (is True) and go having been changed but still evaluating as True (a non-empty list).
In that case, I would define a specific explicit sentinel value
Unchanged = object()
go = Unchanged
while go:
    if go is Unchanged:
        ...
    else:
        ...
Reply
#12
(Nov-30-2018, 07:33 AM)Gribouillis Wrote:
micseydel Wrote:Don't compare boolean values to True or False using ==
But it means that you can write
 if condition is False 
The semantics is slightly different from
 if not condition 
One can reasonably argue that good python code should not rely on such slight differences.
The fuller quote from the link is
Quote:Don't compare boolean values to True or False using ==.

Yes:   if greeting:
No:    if greeting == True:
Worse: if greeting is True:
Reply
#13
Which is a quote from PEP8, which was never meant to be used for all Python code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
It depends on what you want to do. Most of the time, you mean if bool(greeting) is True:. In this case, if greeting: suffices. But it may happen that you really want to know if greeting is the True object. In that case, you need if greeting is True. These are simply two different conditions. It is not a matter of coding style. I agree with pep8 that if greeting is True doesn't test if bool(greeting) is True if that's what you mean. That's why they consider it bad style.
Reply
#15
Hm! It's the same for me.
If a condition is True you take some actions. So if condition evaluates to True condition is True also evaluates to True. You can get it in that way too: bool(condition). The condition is an expression which returns a boolean - True or False.
For me, if condition is True is the same as for index in range(len(items)).
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#16
wavic Wrote:Hm! It's the same for me. If a condition is True you take some actions.
But code is not natural language. The python expression condition is True has a precise semantics which differs from the same expression in natural language. You may find desirable that a programming language resembles natural language but it is only syntactic sugar. Think that Perl was designed as a language providing a "natural" feeling with statements like open IN, $filename or die ...;. What a joke!
Reply
#17
Comparing things in Python drives me nuts, it's not just booleans but everything else too. I feel like JavaScript is much more flexible if that makes sense?
Reply
#18
I absolutely disagree with you. Let's take an empty array for example. In your browser's console (most browsers open it with F12, look at this sort of nonsense:
>> [] == false
<- true
>> if ([]) { "true" }
<- "true"
An empty array is BOTH false, AND evaluates to true.
Here's a fun little video of other wacky things: https://www.destroyallsoftware.com/talks/wat
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020