diff options
author | 2023-11-18 15:01:33 -0500 | |
---|---|---|
committer | 2023-11-18 15:01:33 -0500 | |
commit | 36f1aa2c0dd10153558253a107ad400a325a4a94 (patch) | |
tree | d07bc02ddd86e06f08a9c380c71220c73ee2ef5c | |
parent | Rewriting of non-code sections. (diff) |
New tag to explain why `== True` et al are wrong.
-rw-r--r-- | bot/resources/tags/equals-true.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bot/resources/tags/equals-true.md b/bot/resources/tags/equals-true.md new file mode 100644 index 000000000..d8e1a707e --- /dev/null +++ b/bot/resources/tags/equals-true.md @@ -0,0 +1,24 @@ +--- +embed: + title: "Comparisons to `True` and `False`" +--- +It's tempting to think that if statements always need a comparison operator like `==` or `!=`, but this isn't true. +If you're just checking if a value is truthy or falsey, you don't need `== True` or `== False`. +```py +# instead of this... +if user_input.startswith('y') == True: + my_func(user_input) + +# ...write this +if user_input.startswith('y'): + my_func(user_input) + +# for false conditions, instead of this... +if user_input.startswith('y') == False: + my_func(user_input) + +# ...just use `not` +if not user_input.startswith('y'): + my_func(user_input) +``` +This also applies to expressions that use `is True` or `is False`. |