"if var_X:" vs "if var_X == True:"
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.
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.
"if var_X:" vs "if var_X == True:"
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.
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.
- 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:"
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.
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 > >
Re: "if var_X:" vs "if var_X == True:"
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. =)
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.
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. >_<It is common style to have participles as names for boolean variables
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.
- 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:"
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:
Another common style in Python is to use if container: instead of if len(container) != 0:
< < insert Rick Cook quote here > >
Re: "if var_X:" vs "if var_X == True:"
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.
Well thanks for the assist. That should be all.
Re: "if var_X:" vs "if var_X == True:"
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.
Programming syntax is borrowed from logic syntax.
Re: "if var_X:" vs "if var_X == True:"
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.
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.
Phoenix Star - Persistence, perseverance, ambition.
...
Lost Legends
[VN][Fantasy][Open World] Lost Legends
(Bug Thread) Lost Legends
(Direct Download)
Deviant Art: ...
Lost Legends
[VN][Fantasy][Open World] Lost Legends
(Bug Thread) Lost Legends
(Direct Download)
Discord: saltome
Itch: Phoenix Start
Re: "if var_X:" vs "if var_X == True:"
You may be thinking of:Saltome wrote:if x == True: means exactly that.
Code: Select all
if x is True:
Re: "if var_X:" vs "if var_X == 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.xela wrote: You may be thinking of:Code: Select all
if x is True:
For example:
Code: Select all
x = object
if x:
print (x)
Code: Select all
x = object
if x == True:
print (x)
Phoenix Star - Persistence, perseverance, ambition.
...
Lost Legends
[VN][Fantasy][Open World] Lost Legends
(Bug Thread) Lost Legends
(Direct Download)
Deviant Art: ...
Lost Legends
[VN][Fantasy][Open World] Lost Legends
(Bug Thread) Lost Legends
(Direct Download)
Discord: saltome
Itch: Phoenix Start
Re: "if var_X:" vs "if var_X == True:"
lmao, I actually forgot about how == operator is supposed to work when comparing to True...
Python guidelines suggest:
or
while if x == True is heavily frowned upon... last time I wrote that was over 5 years ago
Python guidelines suggest:
Code: Select all
if x:
Code: Select all
if x is True:
- 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:"
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:
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 > >
Re: "if var_X:" vs "if var_X == True:"
I know, is if you mean it (identity check), : for everything else and screw the == as a general rule when comparing boolsOcelot wrote:Writing if x is True only suggested if you really mean that.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot]