Proper way to use condition?

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
richycapy
Regular
Posts: 56
Joined: Mon May 27, 2019 8:53 pm
Organization: EN Productions
Location: Mexico
Contact:

Proper way to use condition?

#1 Post by richycapy »

Hi, this is a simple question for the more experienced RenPy (and Pyton) programmers:

Which one is the correct:

Code: Select all

if not "chloe_imagen_01" in pictures_taken:
or

Code: Select all

if "chloe_imagen_01" not in pictures_taken:
And from this one:

Code: Select all

if not persistent.chloe_imagen_01:
or

Code: Select all

if persistent.chloe_imagen_01 ==  False:
They all seem to work fine, but im not sure they are all correct :shock:

Thanks

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

Re: Proper way to use condition?

#2 Post by Ocelot »

It is more idiomatic to write if "chloe_imagen_01" not in pictures_taken:. First example always makes me double check operator precedence.

Second one is easy: if you ever compare something to boolean literal, you are doing it wrong.

Code: Select all

# correct
if not persistent.chloe_imagen_01:

# incorrect
if persistent.chloe_imagen_01 ==  False:

# correct
if something_happened:

# incorrect
if something_happened == True:
Technically there are differences between conversion to bool, checking for equality with boolean literal and checking for identity with boolean literal, but I only ever needed implicit bool conversion and rarely simultaneous checking variable type and value by testing it with is True.

Generally you want to go with implicit conversion without checking either equality or identity.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yone28