"if var_X:" vs "if var_X == True:"

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
sunwave
Regular
Posts: 45
Joined: Fri Apr 15, 2016 2:26 pm
Location: Netherlands
Contact:

"if var_X:" vs "if var_X == True:"

#1 Post by sunwave »

So I'm not sure where to post this, since it's not really a "help me with renpy" question but rather a "why does this standard exist" question. However, this is the subforum where most of the coding questions are asked, so I'll just post it here. Or I would have to make an account on some kind of general coding forum, which I can't be bothered with just for this. =P

I've seen several people in several threads note that one shouldn't add "== True" to if-statements, because somehow that's better. To me this suggestions seems very strange. I would have understood the opposite, but not this. Personally I would have suggested the opposite if I had to choose. Sure, it's shorter to just code "if var_X:" instead of the full thing, but to me it seems incomplete. I know how it works, I know what it means, but it still bothers me since it always gives me the dang itch of "if it what" in my mind. The same itch as when someone asks you half a question and you're not sure what they're trying to say. Like "Do you want..." to do what exactly? Or "I'm not sure if.... (silence)... Anyway, let's talk about something else". What are you unsure of?

So... what are the benefits of using "if var_X:" over "if var_X == True:"? Why is the former the generally accepted standard? I see no benefits except that it's shorter. But it's also less clear on what it's doing.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2405
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: "if var_X:" vs "if var_X == True:"

#2 Post by Ocelot »

The semantic of if statement is "convert expression to boolean and proceed if it is true". Usually, you provide expression, which evaluates to boolean or convertible to it. But if a variable is already boolean, you do not need that, comparing boolean to boolean to produce boolean is like writing x = x + 0 or y = y*1 or if (x == 2) == True:: it is superfluous, and programmers hate redundancy.

It is common style to have participles as names for boolean variables. In this case, it is already clear what statement means even for casual non-programmer readers: if box_checked:, if not name_valid(name_entered):.

In addition: it is a decision made by language creator. Many programmers follow these convention, and many style-checking tools will flag your code if you do not comply to them. Thing of those conventions as grammar: you will be understood, even if you do not adhere to it, but it will make understanding you much easier for everyone.

P.S.: I do not remember Python conversion rules, but in C++, for example, if X == true can fail in some cases, when if X would work fine.
< < insert Rick Cook quote here > >

User avatar
sunwave
Regular
Posts: 45
Joined: Fri Apr 15, 2016 2:26 pm
Location: Netherlands
Contact:

Re: "if var_X:" vs "if var_X == True:"

#3 Post by sunwave »

Ah, so simply said, writing "if x == True:" would technically be "if (x == True) is true, then proceed" from a coding standpoint? That does seem a bit silly indeed. Never thought the answer would be that simple. Good thing I asked, for now I know. =)
It is common style to have participles as names for boolean variables
Ah, I didn't do it on purpose but it seems I am following that style too. prologue_finished, trained_with_S, scene_04_survived and stuff. Anyway, thanks for giving an answer that a non-coder can easily understand. Time to clean up my stuff. >_<

PS: Since it's converting the statement to a boolean, I assume the same goes for "if x == 1"? Since I'm using 0's and 1's in my script and not True/False.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2405
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: "if var_X:" vs "if var_X == True:"

#4 Post by Ocelot »

In case of numbers, it is fine to write something like if x == 0:. I personally would use if x / if not x if I would use integers instead of booleans, but implicit int→bool conversion is not something everybody likes. Some argue that it is not obvious that zero converts to False and everything else to True, so, in reality, I would probably not use integers instead of booleans. And if I use integers as proper integers (like training_level variable), I would explicitely test if it is/is not zero/some other number.

Another common style in Python is to use if container: instead of if len(container) != 0:
< < insert Rick Cook quote here > >

User avatar
sunwave
Regular
Posts: 45
Joined: Fri Apr 15, 2016 2:26 pm
Location: Netherlands
Contact:

Re: "if var_X:" vs "if var_X == True:"

#5 Post by sunwave »

I see, I see. In that case, I will try to turn all my variables that only have 0/1 into false/true (since that is what I'm implying for myself anyway) and then just use "if var_X:". Not really necessary, but it's better to start using the "correct" way sooner than later, since otherwise I may get too used to doing it my own way, which could cause trouble later on (If I ever get past my current level of coding skills, that is). My script is not THAT hefty yet so it shouldn't cost me a lot of time.

Well thanks for the assist. That should be all.

figment
Regular
Posts: 33
Joined: Sat Apr 01, 2017 11:02 pm
Contact:

Re: "if var_X:" vs "if var_X == True:"

#6 Post by figment »

It comes from logic and truth tables and all that. If x, then y. You don't have to specify x as true because that's not actually in question. Same with x and y, and x or y.

Programming syntax is borrowed from logic syntax.

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: "if var_X:" vs "if var_X == True:"

#7 Post by Saltome »

Hmm, basically it depends what result you are trying to achieve.
In theory, if x: is cleaner and more effecient. However it is not the same thing as if x==True. Far from it.
if x == True: means exactly that.
if x: means if x is any non-empty value. It could be a boolean, it could be a float, a string, any arbitrary class, or instance, even a method.
For the most part you can get away with it, but you do need to be cautious. By being specific you make sure your program doesn't implode just because it received the wrong kind of variable.
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: "if var_X:" vs "if var_X == True:"

#8 Post by xela »

Saltome wrote:if x == True: means exactly that.
You may be thinking of:

Code: Select all

if x is True:
Like what we're doing? Support us at:
Image

User avatar
Saltome
Veteran
Posts: 244
Joined: Sun Oct 26, 2014 1:07 pm
Deviantart: saltome
Contact:

Re: "if var_X:" vs "if var_X == True:"

#9 Post by Saltome »

xela wrote: You may be thinking of:

Code: Select all

if x is True:
Nah. I guess if x equals 1 or 0, '==' still evaluates to True or False. Which doesn't happen when you use 'is', but it's much more secure.

For example:

Code: Select all

x = object
if x:
    print (x)
^Prints the value of x.

Code: Select all

x = object
if x == True:
    print (x)
^Does not.
Deviant Art: Image
Discord: saltome
Itch: Phoenix Start

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: "if var_X:" vs "if var_X == True:"

#10 Post by xela »

lmao, I actually forgot about how == operator is supposed to work when comparing to True...

Python guidelines suggest:

Code: Select all

if x:
or

Code: Select all

if x is True:
while if x == True is heavily frowned upon... last time I wrote that was over 5 years ago :)
Like what we're doing? Support us at:
Image

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2405
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: "if var_X:" vs "if var_X == True:"

#11 Post by Ocelot »

Writing if x is True only suggested if you really mean that. is means identity, not equality, and would evaluate to True only if value has exact type and refers to the same object (True being a singleton solves some problems with it), prohibiting type conversions.

For general use PEP8 says:
Don't compare boolean values to True or False using == .

Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:
< < insert Rick Cook quote here > >

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: "if var_X:" vs "if var_X == True:"

#12 Post by xela »

Ocelot wrote:Writing if x is True only suggested if you really mean that.
I know, is if you mean it (identity check), : for everything else and screw the == as a general rule when comparing bools :D
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]